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?
5 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
4 Comments
No it's not flawed, but for synchronous code it's typically easier and cleaner to check things with your own code than use a clunky try{}catch(e){}.
example:
var container = document.getElementById("timmy");
container.innerHTML = "I'm timmy!";
If there's no element whose ID is "timmy", we can handle that either like this:
try
{
container.innerHTML = "I'm timmy";
}
catch (error)
{
//handle no container
}
or:
if(container) container.innerHTML = "I'm timmy";
else //handle no container
Another thing to keep in mind is JavaScript applications are event-driven and try/catch blocks in the main part of your program can't catch errors that occur in event callbacks.
example:
sqlConnection.query("SELECT * FROM table",
function(){console.log("queried!");});
that will trigger some native code that runs as your program continues. If an error occurs, you can't expect your program to back-track up to this line so your error handler can do its stuff! Instead, you handle errors in the call-back itself. It is common practice to provide callbacks with any error that occurred by passing it as the first parameter.
sqlConnection.query("SELECT * FROM table",
function(error, data){
if(error) console.log("query failed");
else console.log("queried!");
});
Alternatively, if your code fails in a callback, you can handle it the way I mentioned above.
link.onclick = function(e)
{
var popUp = document.getElementById("popup");
if(!popUp)
return true; //follow link if no pop-up exists
popUp.style.display = "block";
};
1 Comment
The async nature of most javascript code hides the try/catch at a low level and then calls an error callback.
Most heavy code in js is async and uses a callback or promise pattern. The other code is simple and does not require try/catch.
If you dig into js libraries (pick your favorite) you will find the try/catch blocks that wrap much of the heavy app code that executes in modern js applications. These libraries then call the error or fail callback that you see in many js code samples.
1 Comment
I believe that there is more overhead with a try/catch as opposed to an if statement, although i think it only matter when the exception is actually thrown. I use try/catches often, but only if I know there will be a good chance an exception could be thrown.
If used efficiently there is really no reason to not use a try/catch.
Comments
JavaScript doesn't need them much, the reason is, there is hardly any reason to catch errors, because, one error does not stop the entire script. Javascript is event driven, most functions run when a specific event occurs, and it is client side, so if anything goes wrong, it goes wrong, catching and handling the error would be hardly any beneficial. only my view.
try, if you can detect that it will fail before it does.