JavaScript try...catch...finally
JavaScript try...catch...finally
The try...catch...finally statements are used to handle errors without stopping JavaScript from executing.
try statement:-
The try statement is used to define the code block to run i.e, to try.
catch statement:-
The catch statement is used to define a code block to handle any error.
finally statement:-
The finally statement is used to define a code block to run regardless of the result.
throw statement:-
The throw statement is used to define a custom error.
Syntax:-
Its syntax is:-
try {
// try_code - Code block to run
}
catch(error) {
// catch_code - Code block to handle errors
}
finally {
// finally_code - Code block to be executed regardless of the try result
}
Further Explanation:-
Parameter | Description |
try_code | Required. Code block to be tested while executing. |
error | A local reference to the error object. |
catch_code | Optional. Code block to execute if an error occurs. |
finally_code | Optional. Code block to execute regardless of the try result |