JavaScript try/catch/finallyStatement

JavaScript 语句参考手册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 { adddlert("Welcome!"); } catch(err) { document.getElementById("demo").innerHTML = err.message; }

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

try {
    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:

<p>Please enter 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 "is not a number"; if(x > 10) throw "is too large"; if(x < 5) throw "is too small"; } catch(err) { message.innerHTML = "The input value" + err; } }
</script>

Try it Yourself »

Example

finallyThe statement executes regardless of the result of try and catch:

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 "is not a number"; if(x > 10) throw "too large"; if(x < 5) throw "too small"; } catch(err) { message.innerHTML = "The input value" + err; } finally { document.getElementById("demo").value = ""; } }

Try it Yourself »


Related Pages

JavaScript Tutorial:JavaScript Errors

JavaScript Reference:JavaScript throw Statement


JavaScript 语句参考手册JavaScript Statement Reference Other Extensions