Try/Catch

StevenSwiniarski's avatar
Published Jul 23, 2021Updated Dec 10, 2024
Contribute to Docs

The try...catch...finally statement defines three blocks of code. The try block contains code that is tested for possible errors. If an error occurs, the catch block handles the exception. Finally, the finally block contains code that will execute regardless of whether an error occurred, ensuring that cleanup or final actions take place.

All try blocks must be followed by either catch, finally, or both.

Syntax

try {
// Statements here are executed until an exception is thrown
} catch (err) {
// Statements here are executed once an exception is thrown in the try block
} finally {
// Statements here are always executed regardless of any exception
}

The following example shows the try...catch...finally statement in action:

Code
Output
Loading...

The Error Object

In the above, err is an optional variable with an error object for the associated catch block. This object contains information about the thrown exception and is only available in the scope of the catch block.

The error object has two properties:

  • name: Sets or returns an error name. (Type of error)
  • message: Sets or returns an error message. (Description of specific instance)

The following types of error can be returned by the name property:

  • RangeError: A number “out of range” has occurred
  • ReferenceError: An illegal reference has occurred
  • SyntaxError: A syntax error has occurred
  • TypeError: A type error has occurred
  • URIError: An error in encodeURI() has occurred

The throw Statement

The throw statement can be used to throw user-defined exceptions. The custom exception can be any JavaScript type, including String, Number, Boolean or Object, which will be caught by the next outer catch block.

This can be useful for things like validating input:

var input = 25;
try {
if (input < 10) {
throw 'too small!';
} else if (input > 20) {
throw 'too big!';
} else {
console.log('Input was ' + input);
}
} catch (e) {
console.log('Input was ' + e);
}

The throw statement can also re-throw an error object caught by a catch block. This can be useful if only certain types of error should be handled:

try {
// Series of statements
} catch (e) {
if (e instanceof RangeError) {
// Here, any instance of a RangeError exception is handled
} else {
// Re-throw any other exceptions
throw e;
}
}

Codebyte Example

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy