JavaScript try/catch/finally Statement
JavaScript statement reference manual
Example
In the example below, we intentionally wrote a typo in the code of the try block.
This example should have alerted "Welcome!", but instead it shows a spelling error message.
The catch block catches the error from the try block and executes code to handle it:
try
{
adddlert
(
"
Welcome!
"
)
;
}
catch
(
err
)
{
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
err
.
message
;
}
Try it »
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 the code.
The error may be a syntax error, usually a coding mistake or typo made by the programmer. It may also be a misspelling or a missing feature in the language (possibly due to browser differences).
tryThe statement allows us to define a block of code that performs error testing at runtime.
catch The statement allows us to define what try code block to execute when an error occurs in the code block.
finally The statement executes after try and catch regardless of whether an exception occurred.
Note: Both catch and finally are optional, but you must use at least one of them when using a try statement.
Tip: When an error occurs, JavaScript stops executing and generates an error message. Use throw the statement to create a custom message (throw an exception). If you throw and try , catchuse them together, you can control the error message output by the program.
For more about JavaScript errors, see our JavaScript Errors the tutorial.
Browser support
| Statement | |||||
|---|---|---|---|---|---|
| try/catch/finally | Yes | Yes | Yes | Yes | Yes |
Syntax
try {
tryCode - The code block to attempt execution
}
catch(
err
) {
catchCode - The code block that catches errors
}
finally {
finallyCode - The code block that executes regardless of the try / catch result
}
Parameter value
| Parameters | Description |
|---|---|
| tryCode | Required. The code block that checks for errors. |
| err | Required (if using catch). Specifies the local variable that holds the error. This variable can reference an Error object (containing error information, such as "'addlert' is not defined"). If the exception is created via a throw statement, this variable references the object specified in the throw statement (see "More examples") |
| catchCode | Optional. The code block executed if an error occurs in the try statement. This code is not executed if no error occurs in the try statement. |
| finallyCode | Optional. Executes regardless of the result of try / catch. |
Technical Details
| JavaScript version: | 1.4 |
|---|
More examples
Example
This example checks whether the input value is invalid, 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()">Check 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
"
is too large
"
;
if
(
x
<
5
)
throw
"is too small
"
;
}
catch
(
err
)
{
message
.
innerHTML
=
"
The input value
"
err
;
}
}
script
Try it »
Example
finally The 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
"
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 »
Related pages
JavaScript tutorial: JavaScript Errors
JavaScript Reference Manual: JavaScript throw Statement
JavaScript statement reference manual