JavaScript try/catch/finallyStatement
JavaScript Statement Reference
Examples
In the example below, we deliberately made a typo in the code within the try block.
This example should alert "Welcome!", but instead it pops up a spelling error message.
The catch block catches the error in the try block and executes code to handle it:
Try it Yourself »
More examples are included at the bottom of this article.
Definition and Usage
The try/catch/finally statement is used to handle error messages that may occur in code.
Errors can be syntax errors, usually coding errors or typos made by programmers. They can also be spelling errors or missing features in the language (possibly due to browser differences).
tryThe statement allows us to define a block of code to test for errors while it is being executed.
catchThe statement allows us to define whentryan error occurs in the code block, the code block to be executed.
finallyThe statement executes after try and catch regardless of whether there is an exception.
Note:Both catch and finally statements are optional, but you must use at least one when using the try statement.
Tip:When an error occurs, JavaScript stops execution and generates an error message. Usethrowstatement to create a custom message (throw an exception). If youthrowandtry 、 catchuse them together, you can control the error messages output by the program.
For more about JavaScript errors, please refer to ourJavaScript Errorstutorial.
Browser Support
| Statement | |||||
|---|---|---|---|---|---|
| try/catch/finally | Yes | Yes | Yes | Yes | Yes |
Syntax
tryCode - The code block to attempt to execute
}
catch(err) {
catchCode - The code block that catches errors
}
finally {
finallyCode - The code block that executes regardless of the result of try/catch
}
Parameter Values
| Parameter | Description |
|---|---|
| tryCode | Required. The code block that tests for errors. |
| err | Required (if catch is used). Specifies the error applied to a local variable. This variable can reference an Error object (containing the error information that occurred, such as "'addlert' is not defined"). If the exception is created by a throw statement, the variable references the object specified in the throw statement (see "More Examples"). |
| catchCode | Optional. The code block to execute if an error occurs in the try statement. If no error occurs in the try statement, this code will not execute. |
| finallyCode | Optional. Executes regardless of the result of try/catch. |
Technical Details
| JavaScript Version: | 1.4 |
|---|

More Examples
Example
This example detects whether the input value is wrong and throws an exception if it is.
The exception is caught by the catch statement, and a custom message is output:
Try it Yourself »
Example
finallyThe statement executes regardless of the result of try and catch:
Try it Yourself »
Related Pages
JavaScript Tutorial:JavaScript Errors
JavaScript Reference:JavaScript throw Statement
JavaScript Statement Reference Other Extensions