# JavaScript Overview – Throwing Exceptions

Exceptions are used to indicate that an exceptional condition or error has occurred. A throw signals that an error occurred, while the try/catch/finally statement is used by JavaScript to handle an exception or error.

The following demonstrates how to throw an exception within JavaScript:

                if (area <= 0) {
      throw new Error("area must be positive");
      --or--
      throw 'Invalid argument';
      --or--
      throw radius * 3.14;	// not normally used but legal
    }

The try clause defines the block of code that contains the exception to be handled, and is followed by either a catch or finally clause. These two are optional, however, the try block must be followed by at least one of them. The following illustrates how to handle an exception or error in JavaScript:

                try {
      sendAllOutput(stream);
    }
    catch(ex) {				// optional (but one is required)
      console.log (ex);
    }
    finally {				// optional (but one is required)
      stream.close();
    }