Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

It seems like with other languages that support Try/Catch, developers make use of that feature more than they do in JavaScript. Is there a reason for this? Is the JS implementation of Try/Catch flawed?

like image 389
Darrell Brogdon Avatar asked Sep 26 '12 20:09

Darrell Brogdon


People also ask

Should you always use try catch JavaScript?

The try-catch statement should be used any time you want to hide errors from the user, or any time you want to produce custom errors for your users' benefit. If you haven't figured it out yet, when you execute a try-catch statement, the browser's usual error handling mechanism will be disabled.

Why you should not use try catch?

Without a try catch, you run the risk of encountering unhandled exceptions. Try catch statements aren't free in that they come with performance overhead. Like any language feature, try catches can be overused.

Does Try Catch affect performance JavaScript?

As a conclusion, using try-catch in the case where error is never thrown seems to be as efficient as checking any simple condition. If the condition has anything more complex, try-catch is significantly faster.

When should you use try catch in JS?

A try / catch block is basically used to handle errors in JavaScript. You use this when you don't want an error in your script to break your code.


1 Answers

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
like image 186
Chase Avatar answered Sep 25 '22 23:09

Chase