Exception Handling in JavaScript: Introduction to Core Concepts - Trapping exceptions: explaining “try-catch” blocks
(Page 3 of 4 )
True to form, the official JavaScript syntax to create an exception is with the “throw” statement:
throw expression;
And, to handle an exception, it uses the standard combination of “try-catch” blocks:
try {
execute this block
}
catch (error) {
execute this block if an error has occurred
}
It should be noted that in a weakly typed language like JavaScript, an exception can be anything (from plain strings to objects), but unless a specific exception type is launched by a “throw” statement, whenever an error arises, JavaScript makes automatically available an Error object, which is created when an error is triggered.
Additionally, JavaScript supports handling of multiple exceptions through two different structures. First, by using multiple “try-catch” blocks, as follows:
try {
execute this block
}
catch (error type1) {
execute this block if an error of type 1 has occurred
}
catch (error type2) {
execute this block if an error of type 2 has occurred
}
finally {
execute this block after the try block
}
And second, different types of exceptions can be trapped by using multiple “if” statements within a single “catch” block:
try {
execute this block
}
catch (error) {
if(error instanceOf error type1) {
execute this block if an error of type 1 has occurred
}
else if(error instanceOf error type2) {
execute this block if an error of type 2 has occurred
}
else if(error instanceOf error type3) {
execute this block if an error of type 3 has occurred
}
else {
execute this block is none of the above errors occurred
}
}
Although both forms of syntax are valid for dealing with multiple exceptions, the last one listed is by far more popular because of its close intuitive similarity with “if-else if” control blocks found on most programming languages.
Now, allow me to explain how the flow of a program is handled in each pertinent case. In the first case, if an exception is found when executing the code within the “try” block, the JavaScript interpreter will stop the program execution at that point and search for a “catch” block that contains the proper exception handler. After the exception has been handled or no catch blocks present a matching exception handler, then program execution is transferred to the “finally” block.
In the second case, program flow is handled in a similar way. If an exception is encountered during the execution of the “try” block, the program is stopped at that point and JavaScript will look for an exception handler that matches the type of error triggered, within the successive “if-else if” blocks. If the exception has been handled within an “if” block, (and assuming that the program hasn’t been halted), control is returned to the code following the ‘try” statement. Otherwise, if no exception handler is found on each “if” structure, the code within the final “else” block is executed.
To demonstrate how this process works, an example is preferred in all cases, so let’s jump over the next lines to see a few pieces of illustrative code.
Next: A basic example: handling exceptions through a generic mechanism >>
More JavaScript Articles
More By Alejandro Gervasio