I'm starting to learn JavaScript, so far no problem but I have a hard time finding a good explanation of the Exception mechanism in JS.
It seems similar to C++, JS allows to throw about every object, instead of just throwing an Exception object (probably due to it's dynamic nature).
throw 'An error occured.';
works, as well as
throw new Exception('An error occured.');
catch
and finally
both seem to work like their Java equivalent. Still, I don't know what are widely accepted best practices regarding exceptions.
So, for example, is it legit to throw objects of type string, like:
throw 'An error occured';
How would I differentiate between different types of exceptions?
There are mainly two types of exceptions in Java as follows: Checked exception. Unchecked exception.
The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.
Difference Between Checked and Unchecked Exceptions in Java To summarize, the difference between a checked and unchecked exception is: A checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime.
The "best practice", I suppose, is to throw the correct type of Error object related to the problem causing the exception. ECMAScript defines several types of exception object, all of which inherit from Error
. These objects are EvalError
, RangeError
, ReferenceError
, TypeError
and URIError
.
Those constructors are used by native ECMAScript functions, which enables you to do something like this:
try {
// do something
}
catch (e) {
if (e instanceof TypeError) {
// do something else
}
}
Generally, using the throw
statement without using an exception object strikes me as poor practice for several reasons, including:
Error
object, receiving a primitive may result in unexpected side effects or the inability to handle the exception without modifying the handling code. An example of this is the lack of a stack
property on the result from the throw expression.window.onerror
.So, yes, in general stick to properly throwing instances Error
or its inheriting objects.
*nb, IE also does this for types of Error object that aren't constructed directly from Error
. Yes, I know that's stupid, but they fixed it in IE 9 even though they told me it was "by design".
Throwing and catching exceptions is pretty expensive, and with JavaScript, you're primarily going to get exceptions in cases like when you try to parse a JSON string that is malformatted. I would suggest avoiding try/catch as much as possible and instead focus on checking for errors the old fashion way (return types, making sure variables are properly initialized before using them, etc.) since exceptions are less likely to happen here than in C++ or especially Java or .NET.
Andy E's recommendation is a good one for actually handling them, but in general, you should try to write JavaScript code defensively so that you don't even need try/catch. Remember, even JITed JavaScript in Chrome (fastest engine) is still slow as heck compared to Java or C#, to say nothing of C++, so anything that is expensive in those languages is likely to be even more so in JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With