The throw declaration allows you to create an exception.
Example
The Throw statement
How to use throw.
<script type= "Text/javascript" >
var x=prompt ("Enter a number between 0 and 10:", "");
Try
{
if (x>10)
{
Throw "ERR1";
}
else if (x<0)
{
Throw "ERR2";
}
else if (isNaN (x))
{
Throw "ERR3";
}
}
catch (er)
{
if (er== "ERR1")
{
Alert ("error! The value is too high ");
}
if (er== "ERR2")
{
Alert ("error! The value is too low ");
}
if (er== "ERR3")
{
Alert ("error! The value is not a number ");
}
}
</script>
Throw statement
The throw declaration allows you to create an exception. If you use this statement with the attempt ... catch statement, you can control the process and produce an accurate error message.
Grammar
Throw (Exception)
The only exception may be a string, an integer, a Boolean, or an object.
Please note that Throw is writing letters. A JavaScript error is generated in uppercase letters!