YouTip LogoYouTip

Jsref Try Catch

JavaScript try/catch/finally Statement | Rookie Tutorial Image 1: JavaScript Statement Reference Manual JavaScript Statement Reference Manual ## Example In the following example, we intentionally misspell a word in the code within the try block. This example should alert "Welcome!" but instead displays a spelling error message. The catch block captures 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 yourself Β»](#) More examples are provided at the bottom of this article. * * * ## Definition and Usage The try/catch/finally statement is used to handle potential errors in code. Errors may be syntax errors, usually caused by programming mistakes or typos. They may also be spelling errors or missing features in the language (possibly due to browser differences). **try** statement allows us to define a code block to test for errors during execution. **catch** statement allows us to define a code block to execute when an error occurs in the **try** block. **finally** statement executes after try and catch regardless of whether an exception occurred. **Note:** Both catch and finally statements are optional, but at least one must be used when using a try statement. **Tip:** When an error occurs, JavaScript stops executing and generates an error message. Use the (#) statement to create custom messages (throw exceptions). If you use **throw** together with **try** and **catch**, you can control the error messages displayed by your program. For more information on JavaScript errors, see our (#) tutorial. * * * ## Browser Support | Statement | | | | | | | --- | --- | --- | --- | --- | --- | | try/catch/finally | Yes | Yes | Yes | Yes | Yes | * * * ## Syntax try { _tryCode - Code block to attempt execution_} catch(_err_) { _catchCode - Code block to catch errors_} finally { _finallyCode - Code block that executes regardless of the outcome of try / catch_} ## Parameter Values | Parameter | Description | | --- | --- | | _tryCode_ | Required. The code block to check for errors. | | _err_ | Required (if catch is used). Specifies a local variable to reference the error. This variable can refer to an Error object (containing error information such as "'addlert' is not defined"). If the exception was created via a throw statement, this variable refers to 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. This code will not execute if no error occurs in the try statement. | | _finallyCode_ | Optional. Executes regardless of the result of try / catch. | ## Technical Details | JavaScript Version: | 1.4 | | --- | --- | * * * Image 2: Example ## More Examples ## Example This example checks whether the input value is invalid. If invalid, it throws an exception. The exception is caught by the catch statement and a custom message is output:

Please enter a number between 5 and 10:

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"is not a number"; if(x>10)throw"is too large"; if(x<5)throw"is too small"; }catch(err){message.innerHTML = "Input value " + err; }} [Try it yourself Β»](#) ## Example The **finally** statement executes regardless of the outcome of try and catch: 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"is not a number"; if(x>10)throw"is too large"; if(x<5)throw"is too small"; }catch(err){message.innerHTML = "Input value " + err; }finally{document.getElementById("demo").value = ""; }} [Try it yourself Β»](#) * * * ## Related Pages JavaScript Tutorial: (#) JavaScript Reference Manual: (#) * * * Image 3: JavaScript Statement Reference Manual JavaScript Statement Reference Manual
← Jsref VarJsref Throw β†’