Introduction
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.
Use the throw statement to throw an exception. When you throw an exception, expression specifies the value of the exception. Each of the following throws an exception:
Code: JavaScript
throw "Error2"; // generates an exception with a string value
throw 42; // generates an exception with the value 42
throw true; // generates an exception with the value true
Syntax
throw(exception)
The exception can be a string, integer, Boolean or an object.
Note: that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
Example
Code: JavaScript
function userException(msg,errCode)
{
this.description = msg;
this.err = errCode;
}
function divide(operand,divisor)
{
if(divisor==0)
{
var ex = new userException('Division by zero is not possible');
throw ex;
}
return operand/divisor;
}
try
{
var result = divide(123,0); // trying to divide by zero
}
catch(err)
{
alert('Exception: '+err.description+' ('+err.err+')');
}

