Reliable and Recognized JavaScript Certification Online #

During the program execution unexpected, problematic situations may occur. The program should be able to handle these extraordinary cases in a proper way. JavaScript uses exceptions for this purpose, which are a mechanism for handling errors.
When an error or unexpected situation occurs, an exception may be thrown. On the other hand, exceptions can be caught and handled through try-catch statement.


Throwing exceptions

The throw statement

An exception means that an error or other exceptional condition occurs. Exceptions can be thrown both by the JavaScript interpreter when a runtime error occurs and when it is thrown in the program explicitly using throw statement. The syntax of the throw statement is as follows:

throw exception;

To throw an exception use the throw operator following by exception. The exception can be value of any type:

throw 10;
throw true;
throw "Error 1";
throw {name: "MyError"};

When an exception is thrown, interpreter stops immediately usual program execution and jumps to the nearest exception handler. Exceptions handlers are defined by catch clause of try-catch statement. If an exception is thrown and no exception handler is found, the exception is treated as an error.


Error types

Although the exception you throw may be a value of any type, often more effective is to use exceptions defined by JavaScript.
There are following build-in error types:

The Error is the most generic constructor. An Error object has property name that specifies the type of error and property name that provides details about the exception passed to the constructor.

throw new Error("An unexpected error occurred");

You can also create your own types of exception. They may inherit from Error and its derivatives or be independant constructors and objects.

function MyException(message) { = 'MyException';
    this.message = message;

throw new MyException("Not allowed operation");


Handling exceptions

The try-catch statement

To handle exceptions JavaScript language provides try-catch statement. The try clause defines a block of code containing statements whose exceptions should be handled. The catch clause defines block of code following block try and it contains statements that specify how to handle exceptions thrown in the block try. Syntax of try-catch statement is as follows:

try {
    statement; // it can potentially throw an exception
} catch (exc) {
    statement; // exception handling

If a statement in the try block throws an exception, normal flow is stopped and there is jump to the catch block. If exception do not occur, the catch block is omitted.

Below is a real example:

function getDayName(dayNumber) {
    var days = ['Monday', 'Tuesday', 'Wednesday', 'Thuesday', 'Friday', 'Saturday', 'Sunday'];
    var key = dayNumber - 1;
    if (!days[key]) {
        throw new Error("Invalid day number");
    return days[key];

try {
    var dayNumber = 10;
    var dayName = getDayName(dayNumber);
} catch (exc) {
    alert("Something went wrong");


The catch block

The catch block is used to handle exceptions generated in the try block. The catch keyword is followed by round brackets inside of which the identifier exc is located. Through this identifier in the catch block you can refer to the exception that was thrown.

try {
    throw new InternalError("Inwalid data");
} catch (exc) {
    alert('An error occured: ' + + ', details: ' + exc.message); // "An error occured: InternalError, details: Inwalid data"


The finally block

The finally block is used to execute statements after the try and catch blocks. Statements in the finally block are executed regardless of whether an exception was thrown in the try block or not. The finally block is also executed if there is invoked a jump (return, break, continue) in the try block. The jump in the try block is stopped until the finally block is executed and is abandoned if the finally block causes a new jump.
The finally can be used together with the try without a catch part.

try {
    // here may occur an exception
} catch (exc) {
    // here handle the exception if it occurs
} finally {
    // always execute statements here regardless of whether or not an exception occurs

No Comments