The try-catch Statement

JavaScript provides a try-catch statement that is capable of intercepting thrown errors
before they are handled by the browser. The code that might cause an error comes in
the try block and code that handles the error goes into the catch block. For instance:

try {
somethingThatMightCauseAnError();
} catch (ex) {
handleError(ex);
}



When an error occurs in the try block, execution immediately stops and jumps to the
catch block, where the error object is provided. You can inspect this object to determine
the best course of action to recover from the error.
There is also a finally clause that can be added. The finally clause contains code that
will be executed regardless of whether an error occurs. For example:

try {
somethingThatMightCauseAnError();
} catch (ex) {
handleError(ex);
} finally {
continueDoingOtherStuff();
}