Docly Developers
Function Keyword Updated 17 Oct 2026

try statements, catch, [finally]

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

statementscode blockrequired

The main code block or statements to be executed.

catchcode blockrequired

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

finallycode blockoptional

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

Returns

Does not return anything.

Example

Normal JavaScript, 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.");
}