Why isn’t Try/Catch used more often in JavaScript? [closed]

Try taking a look at this article: http://dev.opera.com/articles/view/efficient-javascript/?page=2#trycatch

From the above link:

The try-catch-finally construct is fairly unique. Unlike other constructs, it creates a new variable in the current scope at runtime. This happens each time the catch clause is executed, where the caught exception object is assigned to a variable. This variable does not exist inside other parts of the script even inside the same scope. It is created at the start of the catch clause, then destroyed at the end of it.

Because this variable is created and destroyed at runtime, and represents a special case in the language, some browsers do not handle it very efficiently, and placing a catch handler inside a performance critical loop may cause performance problems when exceptions are caught.

You can also view a similar question here

UPDATE

Adding a link to: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers as it contains useful information regarding V8 and how it handles these (and similar) constructs.

In particular:

Currently not optimizable:

  • Generator functions
  • Functions that contain a for-of statement
  • Functions that contain a try-catch statement
  • Functions that contain a try-finally statement
  • Functions that contain a compound let assignment
  • Functions that contain a compound const assignment
  • Functions that contain object literals that contain proto, or get
    or set declarations.

Likely never optimizable:

  • Functions that contain a debugger statement
  • Functions that call literally eval()
  • Functions that contain a with statement

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)