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 blockrequiredThe main code block or statements to be executed.
catchcode blockrequiredCode block that handles errors or exceptions thrown in the 'statements' block.
finallycode blockoptionalCode 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.");
}The JavaScript above produces this output:
Trying to execute this code.
Error caught: Oops! An error occurred.
This code runs no matter what.