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 known as “exception handling”.

demo throw:

if (true) throw "abc";
// error: Uncaught "abc"

demo throw an Error Object:

if (true) throw new Error(`abc`);
// error: Uncaught Error: abc

Here is a typical usage syntax:

try {
 // do something here that that might throw. or throw yourself here
 if (true) {
  throw "abc";
 }
} catch (xerr) {
 // xerr is a parameter. its value is from throw
 console.log(xerr);
 // abc
}

Here is a typical usage syntax with finally:

try {
 if (true) {
  throw "abc";
 }
} catch (xerr) {
 console.log(xerr);
} finally {
 console.log("Finally block run.");
}
// abc
// Finally block run.

Syntax of try catch finally

The syntax of throw is:

throw expr;

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

try {body} catch(e) {body}

try {body} catch {body}

try {body} catch(e) {body} finally {body}

try {body} finally {body}

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”.

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:
// 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.

The throw statement

throw expression;

throw moves the execution to an outer catch block, if no catch found, abort program.

// 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, usually programing aborts.

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

The 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

try must come with a catch block, elso error.

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

Catch parameter with binding pattern

the catch parameter can use binding pattern. [see JS: Function parameters binding pattern]

try {
 throw new Error("abc");
} catch ({ name, message }) {
 console.assert(name === "Error");
 console.assert(message === "abc");
}

Inside function, return in finally block overrides return/throw

in a function definition body, if you have try/catch/finally blocks, and if finally contains a return (or throw), it overrides any return/throw that happened in try or catch.

function doIt() {
 try {
  throw "try";
 } catch {
  throw "catch";
 } finally {
  // this swallows the throw
  return "finally";
 }
}

console.log(doIt());
// finally

Error object

The throw is often used with a error object.