The try-catch statements are used to catch input/validation errors and runtime errors in the program.

JavaScript code uses Error constructor which can be used as base object to throw an user defined exception. The error objects in all browsers support two properties: name of the constructor the error belongs to and the message which describes the error. There are also vendor specific extensions to Error prototype such as description, name, filename, lineNumber, columnNumber, stack.

Syntax for Error object

new Error([message[, fileName[, lineNumber]]]);

Where:

  • message :  Is description of error. (optional parameter)
  • fileName: The name of the file containing error that called Error() constructor. (optional parameter)
  • lineNumber : Line Number containing Error() constructor invocation.(optional parameter)

Other possible parameters:

  • name : Name of error.
  • description : Error description. Similar to message.
  • columnNumber : Column Number in which error is raised.
  • stack : Stack trace.  

The multiple built-in error types that can be caught in try/catch statements are:

TypeError

This error type is thrown when a value is not of the expected type. The TypeError object represents this error. This method inherits the properties and methods from its prototype chain.

RangeError

This error type occurs when a value is out of range. For instance, when an array is of an illegal length with the Array constructor, or passing bad values to numeric methods such as toExponential(), toFixed() …etc. The  RangeError object represents this error. This method inherits the methods from its prototype chain and allows addition of properties to its object prototype.

EvalError

This error type occurs when there is an error in eval() function. The EvalError object indicates this error. This object does not have its own methods, it inherits methods from its prototype.

InternalError

This error type occurs when there is an error occurring internally in the JavaScript engine. For instance when some code is too large like, too much of recursion, array initializer too large, etc... This is non-standard feature and may not work for all the browsers. The InternalError object is not encouraged to use in production. It’s methods and properties are inherited from prototype chain of Error.

ReferenceError

This error type is said to occur when de-referencing a non-existent variable. The ReferenceError object represents this error. Its methods are inherited from prototype chain of Error and allows the addition of properties to its object prototype.

SyntaxError

This error type occurs when parsing a code with a syntax error. The SyntaxError object represents this error. This object inherits methods from it’s prototype and allows the properties to be added to the object prototype.

URIError

This error type occurs when an invalid parameter is passed to a global URI handling functions (encodeURI(), decodeURI(),decodeURIComponent(),encodeURIComponent(), escape(), and unescape()). The URIError object represents this error. This object inherits methods from its prototype and allows the properties to be added to the object prototype.

Example of the Error object and error types

 

›› go to examples ››