CKTIS / JavaScript
try — exception handling
The try statement defines a block whose exceptions can be handled by catch. An optional finally runs finalization code even during return or exception propagation. A catch, finally, or both is required.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
try { statements } catch (error) { handler }
try { statements } finally { cleanup }
try { statements } catch (error) { handler } finally { cleanup }
// Optional catch binding:
try { statements } catch { handler }try is a reserved word that begins an exception-handling statement. Its block must be followed by catch, finally, or both, in that order. Braces are required; try { ... } alone is invalid.
When execution of the try block throws an exception, the remaining statements in that block are skipped. catch (error) binds the thrown value to the local parameter error; the value need not be an Error object. If the parameter is unnecessary, use catch { ... }. Without an exception, the catch block is skipped. A new exception from catch is not caught by that same catch.
finally executes before this statement finishes, including when leaving through return or an exception. If finally completes normally, any pending return or exception is preserved. That also holds if a throw inside finally is caught within the block and the block then completes normally. A return that exits finally or an exception that escapes it overrides the pending return or exception. Avoid overriding it in cleanup code.
In the example, throw skips recording "unreachable", catch records the message, and finally appends the last item.
Examples
const events = [];
try {
events.push("try");
throw new Error("stop");
events.push("unreachable");
} catch (error) {
events.push(error.message);
} finally {
events.push("finally");
}
console.log(events.join(" | "));Result
try | stop | finally
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.