September 19, 2002 - Creating and Throwing Error Object Exceptions | WebReference

September 19, 2002 - Creating and Throwing Error Object Exceptions

Yehuda Shiran September 19, 2002
Creating and Throwing Error Object Exceptions
Tips: September 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

The simplest form of an exception is the string. You just throw a string from a try or a catch block, to be caught by a catch block at the same or higher level of try...catch nesting. For example, you can throw the error message "Error 325 from inner try block" as an exception.

But JScript .NET supports a more structured exception: the Error object. An Error object is thrown by the system whenever there is a run-time error. You can throw your own Error objects, and thus keep all exceptions of the Error type. You create Error objects with the Error constructor:

  function Error([number : Number [, description : String ]])
where:

number is a numeric value assigned to the exception. It is optional.
  • description is a brief string that describes the exception. It is optional.

    You can create a new Error object, populate it, and throw it, all in the same statement. For example:

      throw new Error(35,"null object, call your vendor");
    To learn more about exception handling, go to Column 118, JScript .NET, Part XII: Exception Handling.