The try...catch..finally statement specifies a block of code to try along with a response should an error occur. The try statement contains one or more try blocks, and ends with at least one catch and/or a finally clause.

try...catch:

try {
   throw new Error('my error');
} catch (err) {
  console.error(err.message);
}

// Output: my error

try...finally:

try {
   throw new Error('my error');
} finally {
  console.error('finally');
}

// Output: finally

When you don't use a catch statement, the error is not "caught", even though the code in the finally block is executed. Instead, the error will continue to the upper try block (or main block).

try...catch...finally:

try {
   throw new Error('my error');
} catch (err) {
  console.error(err.message);
} finally {
  console.error('finally');
}

// Output:
// my error
// finally

Typical usage:

try {
   openFile(file);
   readFile(file)
} catch (err) {
  console.error(err.message);
} finally {
  closeFile(file);
}

Nested try...catch:

You can also:

  • Nest a try-catch statement inside a try block.

You can nest a try...catch statement within a try block. For example, to throw an error upwards:

try {
  try {
    throw new Error('my error');
  } catch (err) {
    console.error('inner', err.message);
    throw err;
  } finally {
    console.log('inner finally');
  }
} catch (err) {
  console.error('outer', err.message);
}

// Output: 
// inner my error 
// inner finally 
// outer my error