JavaScript Error Handling

JavaScript Error Handling: Error Object Hierarchy

The powerful JavaScript language and development platform provides a number of useful core object constructors that allow for simple exception and error administration. JavaScript error handling is typically performed through the generic Error object, or from any of a number of core error constructors.

Here we see the basic list of built-in error objects provided by JavaScript:

Below we’ll examine each of the core error constructors provided by JavaScript, looking at simple examples and descriptions for what can cause each type of error to occur.


At the most basic level, the [Error] object is thrown when a runtime error occurs. Error is best used for a generic, user-defined error type that should be thrown, which may not match an existing core error constructor:

Output: Error: Uh oh.


The [EvalError] object represents an error that occurs during the use of the global eval() function. While the EvalError is no longer thrown by newer versions of JavaScript, and is thus deprecated, it remains intact for backward compatibility purposes.

Here is an example of throwing an EvalError object error:


The [InternalError] object is thrown when the JavaScript engine itself experiences an internal error. While the InternalError object is considered non-standard and thus shouldn’t be relied upon in production environments, it can be utilized in some cases to detect engine failure points.

Executing the following code will produce an InternalError in some browsers or environments (while others will instead produce the RangeError, as described below):

Output: InternalError: too much recursion.


A [RangeError] indicates when an error occurs when an argument value is outside of the allowed bounds for a particular method’s parameter. This can be seen when passing improper values to built-in methods such as Number.toFixed():

Output: RangeError: toFixed() digits argument must be between 0 and 20(…).


The [ReferenceError] object represents an error when a reference is made to a non-existent variable:

Output: ReferenceError: myUndefinedVariable is not defined.


As the name implies, the [SyntaxError] object appears when an error occurs trying to execute code that is syntactically invalid. For example, here we can catch a SyntaxError object by trying to use eval() on invalid code:

Output: SyntaxError: Unexpected identifier.


The [TypeError] object occurs from an error when a value doesn’t match the expected data type.

Here we’re creating a new variable foo, assigning it to the value null, and then attempting to call the missing foo.myMethod property, which doesn’t exist:

Output: TypeError: Cannot read property 'myMethod' of null.


Finally, the [URIError] object represents an error that occurred when one of JavaScript’s global URI functions (such as decodeURI()) is improperly called:

Output: URIError: URI malformed.

These built-in exception classes are helpful JavaScript error handling tools! Used with Airbrake’s JavaScript Error Handler your debugging process will be a breeze. Good luck!