JS: Throw Try Catch Finally

By Xah Lee. Date: . Last updated: .

Throw, Try, Catch, Finally

keywords throw, try, catch, finally, are for dealing with errors. often called “exception handling”.

Here is a typical usage syntax:

try {
    // call a function that might throw.
    // or throw yourself here
    if ( something ) {
        throw "XYZ";
    }
}
catch (abc) {
    // abc is a parameter. its value is from throw
    console.log (abc); // prints XYZ
}
finally {
    console.log ("Finally block run!");
}

How to Use “throw”, “try/catch/finally”

The syntax of “throw” is:

throw expr;

The syntax of “try/catch/finally” is any of the following:

When the “throw” statement is run, it redirect code execution to a outer “catch” block of a “try/catch/finally” statement. Then, run the “finally” block if there is one.

If no “try/catch/finally” is found, JavaScript stops running and prints the expression passed by “throw”.

“throw” is the most important in “throw/try/catch/finally”. Without “throw”, the “try/catch/finally” is useless.

The purpose of “try” block is just to contain “throw” (or contain function calls that may “throw”.).

When the “try/catch/finally” is run, code in the “try” block is executed. If throw happens in “try” block, the “catch” block is run, else skipped. The “finally” block (if any) is always run.

try {
    console.log (3);
} catch(e) {
    console.log (4);
}
finally {
    console.log (5);
}

// result:
// prints 3 5

The “throw” and “try/catch/finally” block can be used without each other. But, typically, “throw” is used inside a “try” block. And you should do so in your code.

「throw」 Statement

throw is like goto. It moves the execution to another place in code. The syntax is this:

throw expression;

// example of throw in a function, caught outside of the function

function ff () {
    throw 4;
}

try {
    ff();
} catch(ee) {
    console.log (ee);
};

When “throw” is called, execution jumps to the nearest outer block of catch in a “try/catch/finally” block. If the nearest outer block is a function block, then JavaScript tries to find the outer “catch” at where the function is called.

It repeats the process of jumping to the nearest outer block to find a “try/catch/finally”.

If no “try/catch/finally” is found, it's a error, JavaScript stops running and argument of “throw” is printed.

If a “try/catch/finally” is found, the program flow continues at the “catch” block, and the argument of “throw” is passed to the parameter of “catch”.

// throw can be run by itself
throw 3;

// this is un-catched throw.
// js exits and prints 3
// throw in a try/catch block

try {
    throw 3;
} catch(e) {
    console.log (e);
}

// exits normally.
// result is printing 3
// throw in a try/catch block

try {
    if ( false ) {
        throw 3;
    };
} catch(e) {
    console.log (e);
}

// throw is never executed

“try” Statement

The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exception-handling code. When a catch clause catches an exception, its CatchParameter is bound to that exception.

ECMAScript 5.1 §12#sec-12.14

Here is a example of “try”, trying to call a function that may not exist.

try {
    yyy();
} catch(e) {
    console.log ("aa");
}

console.log ("bb");

// result is printing aa then bb

// without the try block, calling a non-existent function will exit abnormally, and bb won't be printed

Error Object

The throw is often used with a error object. That is, in throw expr;, the expr is a error object.

// throw an error object

try {
    throw new Error("xyz is wrong.");
} catch(ee) {
    console.log (ee);
};

However, Error object in JavaScript is independent of the “throw/try/catch” statement.

JavaScript, Try, Catch, Throw, Error

BUY ΣJS JavaScript in Depth