Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 44 lines (32 sloc) 2.06 kb

Try-Catch-Finally

Javascript's try-catch-finally statement works very similarly to the try-catch-finally encountered in C++ and Java. First, the try block is executed until and unless the code in it throws an exception (whether it is an explicit throw statement, the code has an uncaught native exception, or if the code calls a function that uses throw).

Example:

console.log("entering try-catch statement");

try {
  console.log("entering try block");
  throw "thrown message";
  console.log("this message is never seen");
}
catch (e) {
  console.log("entering catch block");
  console.log(e);
  console.log("leaving catch block");
}
finally {
  console.log("entering and leaving the finally block");
}

console.log("leaving try-catch statement");

Results:

  • entering try-catch statement
  • entering try block
  • entering catch block
  • thrown message
  • leaving catch block
  • entering and leaving the finally block
  • leaving try-catch statement

If the code doesn't throw an exception, then the whole try block is executed. If the code threw an exception inside the try block, then the catch block is executed. Last of all, the finally block is always executed, subsequent to the other blocks but prior to any subsequent code located outside of the try-catch-finally blocks. The finally block will just about always execute, no matter what kind of throwing, catching, or returning one might be trying to do inside the try or catch blocks.

Note that you can omit the catch or finally block, but one of them must be present.

But wait, isn't it Node.js convention to not use try-catch?

In the core node.js libraries, the only place that one really needs to use a try-catch is around JSON.parse(). All of the other methods use either the standard Error object through the first parameter of the callback or emit an error event. Because of this, it is generally considered standard to return errors through the callback rather than to use the throw statement.

Something went wrong with that request. Please try again.