JScript  

throw Statement

Generates an error condition that can be handled by a try...catch…finally statement.

throw exception 

The required exception argument can be any expression.

Remarks

The following example throws an error based on a passed-in value, then illustrates how that error is handled in a hierarchy of try...catch…finally statements:

function TryCatchDemo(x){
   try {
      try {
      if (x == 0)   // Evalute argument.
         throw "x equals zero";   // Throw an error.
      else
         throw "x does not equal zero";   // Throw a different error.
      }
      catch(e) {   // Handle "x = 0" errors here.
         if (e == "x equals zero")   // Check for an error handled here.
            return(e + " handled locally.");   // Return object error message.
         else   // Can't handle error here.
            throw e;   // Rethrow the error for next
      }   // error handler.
   }
   catch(e) {   // Handle other errors here.
      return(e + " handled higher up.");   // Return error message.
   }
}
document.write(TryCatchDemo(0));
document.write(TryCatchDemo(1));

Requirements

Version 5

See Also

try...catch Statement