JavaScript performance library

Learn how to work with exceptions in JavaScript in this short tutorial.

Exceptions in JavaScript have a very similar syntax as in other languages with C-like syntax:

try {
    throw new Error("Error happened inside 'try' block!");
} catch(e) {
    alert(e.message); // = Error happened inside 'try' block!
} finally {
    alert("Finally block"); // Executed no matter if an exception was thrown or not
}

Structure of try…catch…finally blocks

Exception handling takes place in three blocks (one block is optional), as seen o the code listing above. Those blocks are:

try

The try block contains the code that can possibly generate errors which you would want to detect and handle.

When an error is detected inside try block then execution of the code inside the block is stopped and execution flow of the program is moved to catch block.

User can report errors (technically saying throw exception) in his own code  by using throw keyword which is described in later part of this article.

catch

The catch block contains code that handles reported errors (exceptions).

This block is executed only when an exception is thrown in the try block. If no exception is thrown in that block, the catch block is not executed.

finally

The finally block is optional and is placed after catch block.

This block is executed after execution of try...catch blocks is complete. It is executed no matter if any exception was thrown or not.

Throwing own exceptions

A custom exception can be thrown by using any object (like a constructor function, object literal or object variable).

var MyException = function(message) {
    this.name = "MyException";
    this.message = message;
}

try {
    throw new MyException("This is MyException");
} catch(e) {
    alert(e.name+": "+e.message); // = MyException: This is MyException
} 

// ---

var someObject = {
    name: "someObject",
    message: "I am someObject"
}

try {
    throw someObject;
} catch(e) {
    alert(e.name+": "+e.message); // = someObject: I am someObject
} 

// ---

try {
    throw {
        name: "ObjectLiteral",
        message: "I am ObjectLiteral"
    }
} catch(e) {
    alert(e.name+": "+e.message); // = ObjectLiteral: I am ObjectLiteral
} 

// ---

try {
    throw 666;
} catch(e) {
    alert(e); // = 666
}

Every standard exception object has name and message parameter. While you are not forced to introduce those parameters in your exceptions, it’s a good practice to implement them.

name property contains short name of exception, ie. “NullPointerException“.

message property contains descriptive, human readable description of exception, ie. “Cannot divide a number by zero“.

Catching different exceptions

In JavaScript the catch block is only one and it does not catch only one given type of exception. Instead, the catch block catches every exception that is thrown in try block.

To get an idea what exception was thrown you can use the name property of exception in conditional blocks:

try {
    var something = new nonExistentConstructor();
    
    throw {
        name: "MyOwnException",
        message: "I am a very bad, bad error"
    }
} catch(e) {
    if(e.name === "MyOwnException") {
        alert("A very bad error happened");
    } else if(e.name === "ReferenceError") {
        alert("You have tried to invoke undefined constructor");
    } else {
        alert("An unkown exception was thrown");
    }
}

// = You have tried to invoke undefined constructor

You can also use the instanceof keyword, or any other object parameter. However, it is a common and good practice to introduce and check the name property for that purpose.