Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

96 The try...catch Statement

The intent of try...catch is to catch errors in specific tasks before they cause problems. The try...catch statement is most effective with one to a few lines of code intended to do one specific thing within a function. This make debugging easy because very specific errors can be written to the console and handled in a way which won’t interfere with the rest of the script. The try...catch statement works basically the same as the if statement.

try {
	<code to be attempted>
} catch ( error ) {
	<code to handle the error>
}

In the above example: