English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
try ... catchThe statement marks the block of statements to be tried and specifies the response when an exception (error) is thrown.
The try statement consists of a try block that contains one or more statements. {} must always be used for a single statement.
There must be at least one catch clause or finally clause. This provides three forms for the try statement:
try...catch
try...finally
try...catch...finally
UsetryThe statement can define a code block that will be tested for errors during execution.
catchThe statement allows you to define a code block that will be executed if an error occurs in the try block.
finallyIn the declaration, the code you can execute after try and catch, regardless of the result.
Note: Both catch and finally statements are optional, but when using the try statement, you must use one of them (if you cannot use both).
UsethrowThe statement creates a custom error (throws an exception). If you willthrowWhen used with try and catch, it allows you to specify the program flow and generate custom error messages (see the 'More Examples' below).
You can find more information about ourJavaScript exception tutorialLearn more about exceptions in the
try { //try_statements-The statements to be tried } catch(err) { //catch_statements-Error handling statements } finally { //finally_statements-Regardless of the try / Statements to be executed regardless of the result of the catch }
try { aaalert("Hello world"); } catch(err) { document.getElementById("result").innerHTML = err; }Test and see‹/›
All browsers fully support the try ... catch statement:
Statement | |||||
try...catch | is | is | is | is | is |
Parameter | Description |
---|---|
try_statements | The statements to be executed. |
err | Identifier of the exception object associated with the catch clause. SeeError object. |
catch_statements | (Optional) Statements to be executed when an exception is raised in the try block. This code block will never be executed if no error occurs. |
finally_statements | (Optional) Statements to be executed after the try statement is completed. These statements will always be executed, regardless of whether an exception is raised or caught. |
JavaScript version: | ECMAScript 3 |
---|
InfinallyIn the declaration, you can execute code, after try and catch, regardless of the result:
try { aaalert("Hello world"); } catch(err) { document.getElementById("result").innerHTML = err; } finally { document.getElementById("result").innerHTML += "<h3>Finally statement executed</h3>"; }Test and see‹/›
catchandfinallyStatements are optional, but when using the try statement, you need to use one of them (if you cannot use them together):
try { aaalert("Hello world"); } finally { document.getElementById("result").innerHTML = "Final execution statement"; }Test and see‹/›
UsethrowStatement creates a custom error (raises an exception):
function getRectArea(width, height) { if (isNaN(width) || isNaN(height)) { throw "The parameter is not a number!"; } } try { getRectArea(5, 'Z'); } catch(err) { document.getElementById('para').innerHTML = err; }Test and see‹/›
Check the input, if the value is incorrect, then raise an exception (err).catchCatch exceptions (err) and display custom error messages:
var x = document.querySelector("input").value; try { if(x == "") throw "is Empty"; ) throw "Not a Number"; if(x > 10) throw "too High"; if(x < 5)throw "too Low"; } catch(err) { document.getElementById("para").innerHTML = "Input" + err; }Test and see‹/›
JavaScript Tutorial:JavaScript Exceptions
JavaScript Reference:JavaScript Error Object
JavaScript Reference:JavaScript throw Statement