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.


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.
         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.


Version 5

See Also

try...catch Statement