JavaScript try/catch/finally statement
- Previous Page throw
- Next Page var
- Go Up One Level JavaScript Statement Reference Manual
Definition and Usage
The try/catch/finally statement handles partial or all errors that may occur in a code block while still running the code.
Errors may be caused by coding errors made by programmers, errors caused by incorrect input, and other unforeseen situations.
The try statement allows you to define a code block that is tested for errors during execution.
If an error occurs in the try block, the catch statement allows you to define a block of code to be executed.
The finally statement allows you to execute code after try and catch, regardless of the result.
Note:Both catch and finally statements are optional, but one of them (or both) must be used when using the try statement.
Tip:When an error occurs, JavaScript usually stops and generates an error message. Use the throw statement to create custom errors (throw exceptions). If you use throw with try and catch, you can control the program flow and generate custom error messages.
For more knowledge about JavaScript errors, please learn the JavaScript error tutorial.
Example
In this example, we made a typo in the code (in the try block).
This example should prompt "Welcome guest!", but there is a spelling error in the alert.
The catch block will catch the error and execute code to handle it:
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> try { adddlert("Welcome guest!"); } catch(err) { document.getElementById("demo").innerHTML = err.message; } </script> </body> </html>
More TIY examples are available at the bottom of the page.
Syntax
try { tryCode - A block of code to try } catch(err) { catchCode - A block of code to handle errors } finally { finallyCode - A block of code to be executed regardless of the try / catch result }
Parameter Value
Parameter | Description |
---|---|
tryCode | Required. A block of code to be tested for errors during execution. |
err | Required, if used with catch. Specifies the local variable that refers to the error. This variable can refer to the Error object (which contains information about the error, such as the message "'addlert' is not defined"). If the exception is created by a throw statement, then the variable refers to the object specified in the throw statement (see "More Examples"). |
catchCode | Optional. A block of code to be executed if an error occurs in the try block. It will never be executed if no error occurs. |
finallyCode | Optional. A block of code that must be executed regardless of the result of try / catch. |
Technical Details
JavaScript version: | ECMAScript 3 |
---|
More examples
Example
This example checks the input. If the value is incorrect, it throws an exception (err).
The catch statement catches exceptions (err) and displays a custom error message:
<!DOCTYPE html> <html> <body> <p>Please input a number between 5 and 10:</p> <input id="demo" type="text"> <button type="button" onclick="myFunction()">Test Input</button> <p id="message"></p> <script> function myFunction() { var message, x; message = document.getElementById("message"); message.innerHTML = ""; x = document.getElementById("demo").value; try { if(x == "") throw "is Empty"; if(isNaN(x)) throw "not a number"; if(x > 10) throw "too high"; if(x < 5) throw "too low"; } catch(err) { message.innerHTML = "Input " + err; } } </script> </body> </html>
Example
The finally statement allows you to execute code after both try and catch, regardless of the outcome:
function myFunction() var message, x; message = document.getElementById("message"); message.innerHTML = ""; x = document.getElementById("demo").value; try { if(x == "") throw "Empty"; if(isNaN(x)) throw "Not a number"; if(x > 10) throw "Too high"; if(x < 5) throw "Too low"; } catch(err) { message.innerHTML = "Error: " + err + "."; } finally { document.getElementById("demo").value = ""; } }
browser support
statement | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
try/catch/finally | Support | Support | Support | Support | Support |
Related Pages
JavaScript Tutorial:JavaScript Errors
JavaScript Reference Manual:JavaScript throw Statement
- Previous Page throw
- Next Page var
- Go Up One Level JavaScript Statement Reference Manual