Core JavaScript 1.5 Guide:Exception Handling Statements:throw Statement
From MDC
[edit] throw Statement
Use the throw statement to throw an exception. When you throw an exception, you specify the expression containing the value to be thrown:
throw expression;
You may throw any expression, not just expressions of a specific type. The following code throws several exceptions of varying types:
throw "Error2";
throw 42;
throw true;
throw {toString: function() { return "I'm an object!"; } };
Note: You can specify an object when you throw an exception. You can then reference the object's properties in the
catch block. The following example creates an object myUserException of type UserException and uses it in a throw statement.// Create an object type UserException
function UserException (message)
{
this.message=message;
this.name="UserException";
}
// Make the exception convert to a pretty string when used as
// a string (e.g. by the error console)
UserException.prototype.toString = function ()
{
return this.name + ': "' + this.message + '"';
}
// Create an instance of the object type and throw it
throw new UserException("Value too high");