JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Error Handling

When executing JavaScript code, different errors can occur.

Errors can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things.

Therefore to handle errors JavaScript provide 4 keywords:

  • The try statement lets you test a block of code for errors
  • The catch statement lets you handle the error
  • The throw statement lets you create custom errors
  • The finally statement lets you execute code, after try and catch, regardless of the result

In this example we have written "alert" as "aaalert" to deliberately produce an error:

Run code

When an error occurs, JavaScript will normally stop, and create an Error object with two properties: name and message.


JavaScript try...catch Statement

The try statement allows you to define a block of code to be tested for errors while it is being executed.

The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

The JavaScript statements try and catch come in pairs:

try {
   try_statements - Statements to try
}
catch(err) {
   catch_statements - Statements to handle errors
}

JavaScript throw Statement

The throw statement throws a user-defined exception.

The throw statement allows you to create a custom error. Technically this is known as "throw an exception".

The exception can be a JavaScript String, a Number, a Boolean or an Object:

If you use throw together with try and catch, you can control program flow and generate custom error messages.

In this example the getRectArea() will throw a custom error if you pass any non-numeric argument:

Run code

Input Validation Example

In this example, if the value is wrong, an exception (err) is thrown. The exception (err) is caught by the catch statement and a custom error message is displayed:

Run code

JavaScript finally Statement

The finally statement lets you execute code, after try and catch, regardless of the result.

Run code

The catch and finally statements are both optional, but you need to use one of them (if not both) while using the try statement:

Run code

JavaScript Error Object

JavaScript has a built in Error object that provides error information when an error occurs.

The error object provides two useful properties: name and message.


Error Object Properties

The following table lists the properties of the Error object:

Property Description
name Sets or returns an error name
message Sets or returns an error message

Error Types

There are seven different values can be returned by the error name property:

Type Description
EvalError Representing an error that occurs regarding the global function eval()
InternalError Representing an error that occurs when an internal error in the JavaScript engine is thrown
RangeError Representing an error that occurs when a numeric variable or parameter is outside of its valid range
ReferenceError Representing an error that occurs when de-referencing an invalid reference
SyntaxError Representing a syntax error that occurs while parsing code in eval()
TypeError Representing an error that occurs when a variable or parameter is not of a valid type
URIError Representing an error that occurs when encodeURI() or decodeURI() are passed invalid parameters

The following section describes each one of these error type in more detail.


EvalError

An EvalError is thrown when an error occurs during the execution of code via eval() function.

But, this error is not thrown by JavaScript anymore, however this object still remains for backward compatibility.

Newer versions of JavaScript do not throw EvalError. Use SyntaxError instead.


RangeError

A RangeError is thrown when you use a number that is outside the range of allowable values.

For example, creating an array with a negative length will throw RangeError:

Run code

ReferenceError

A ReferenceError is typically thrown when you try to reference or access a variable or object that doesn't exist.

Run code

SyntaxError

A SyntaxError is thrown at runtime if there is any syntax problem in your JavaScript code.

Run code

TypeError

A TypeError is thrown when a value is not of the expected type.

Run code

URIError

A URIError is thrown when you specified an invalid URI (Uniform Resource Identifier).

Run code

Complete Error Reference

For a complete properties and methods reference, visit our JavaScript Error Reference.

The reference section contains descriptions and examples of all Error properties and types.