try statements, catch, [finally] Last updated: 17. Oct 2026

Keyword

Runs a block of code and catches errors with catch. An optional finally block runs whatever the outcome.

The catch variable holds exactly what was thrown:

  • throw new Error("x") gives an Error object with name "Error" and message "x".
  • throw "text" gives the text itself, so e.message is undefined.
  • throw { code: 403 } gives the object with all its fields.
  • Errors raised by the engine (for example reading a property of undefined in a .js file) arrive as a TypeError.

e.message contains only the message text, and there is no e.stack. Note: new Error("x") throws immediately even when it is not preceded by throw — to create an error object without throwing, call Error("x") without new.

Parameters

Name Type Description
statements code block

The main code block or statements to be executed.

catch code block

Code block that handles errors or exceptions thrown in the 'statements' block.

finally (optional) code block

Code block that executes after the try and catch blocks, regardless of the result.

Returns

Does not return anything.

Example

Code example (JS)

JS is normal JavaScript either running in the browser or on the Docly™ server.
try {
  // Code that may throw an error
  write("Trying to execute this code.\n");
  throw new Error("Oops! An error occurred.");
} catch (error) {
  // Code to run if an error occurs
  write("Error caught: " + error.message + "\n");
} finally {
  // Code that will run regardless of the try / catch outcome
  write("This code runs no matter what.");
}

Output

The JS code above produces the output shown below:
Trying to execute this code.
Error caught: Oops! An error occurred.
This code runs no matter what.