The throw statement allows you to create an exception in JavaScript. It is used together with try...catch blocks to control program flow and generate error messages when exceptions are thrown. The throw statement syntax is "throw(exception)" where the exception can be a string, number, boolean, or object. An example checks the value of a variable against thresholds, throwing different errors messages using try...catch if the value is too high, too low, or not a number.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
12 views2 pages
Javascript Statement: Throw
The throw statement allows you to create an exception in JavaScript. It is used together with try...catch blocks to control program flow and generate error messages when exceptions are thrown. The throw statement syntax is "throw(exception)" where the exception can be a string, number, boolean, or object. An example checks the value of a variable against thresholds, throwing different errors messages using try...catch if the value is too high, too low, or not a number.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2
JavaScript Throw Statement
The throw statement allows you to create an exception.
The Throw Statement The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages. Syntax throw(exception) The exception can be a string, integer, Boolean or an object. ote that throw is written in lowercase letters. !sing uppercase letters will generate a JavaScript error" #xample The example below determines the value of a variable called x. If the value of x is higher than $%, lower than %, or not a number, we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed& #xample <html> <body> <script type="text/javascript"> var x=prompt("Enter a number between 0 and 0!"""")# try $ i%(x>0) $ throw "Err"# & else i%(x<0) $ throw "Err'"# & else i%(is(a((x)) $ throw "Err)"# & & catch(er) $ i%(er=="Err") $ alert("Error* +he value is too hi,h")# & i%(er=="Err'") $ alert("Error* +he value is too low")# & i%(er=="Err)") $ alert("Error* +he value is not a number")# & & </script> </body> </html>