JavaScript throw statement
JavaScript Statements Reference Manual
Examples
In this case the value of the detection of the input variables. If the value is wrong, throws an exception (err)
Exception (err) by the catch phrase to capture and customize output an error message:
<Html>
<Body>
<P> Please input a number between 5 and 10: </ p>
<Input id = "demo" type = "text">
<Button type = "button" onclick = "myFunction ()"> Test 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 "too high";
if (x <5) throw "too low";
}
catch (err) {
message.innerHTML = "Input" + err;
}
}
</ Script>
</ Body>
</ Html>
try it"
Definition and Usage
throw statement throws an error.
When an error occurs, JavaScript execution will stop and throw an error message.
The technical term to describe this situation is: JavaScript will throw an error.
throw statement creates custom error.
The technical term is: an exception is thrown.
JavaScript exception can be strings, numbers, or logical values objects:
throw 500; // throw numbers
If the throw and try and catch used together, you can control program flow and generate a custom error message.
More JavaScript error message, please read our JavaScript Errors tutorial.
Browser Support
Statements | |||||
---|---|---|---|---|---|
throw | Yes | Yes | Yes | Yes | Yes |
grammar
Parameter Value
parameter | description |
---|---|
expression | have to. Thrown. It can be a string, number, logical value or object. |
technical details
JavaScript version: | 1.4 |
---|
Related Pages
JavaScript Tutorials: JavaScript error
JavaScript Reference Manual: JavaScript the try / the catch / a finally statement
JavaScript Statements Reference Manual