Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

catch — handling exceptions

The catch keyword introduces a handler for an exception thrown while a try block executes. Its optional parameter exposes the thrown value. After the exception is handled, execution can continue after the try...catch statement.

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 {
  // protected statements
} catch (error) {
  // handle the thrown value
}

try {
  // protected statements
} catch {
  // handle without binding the thrown value
}

catch is a keyword and an unconditionally reserved word: it cannot be a variable name. It forms a clause of a try statement, not a standalone statement. Both the try and catch blocks require braces.

When execution of the try block ends by throwing an exception, the remaining statements in that block are skipped and control moves to catch. If no exception occurs, the catch block is skipped. The parameter in catch (error) is local to the handler and receives the thrown value, which need not be an Error object. A destructuring pattern can replace the identifier.

If the thrown value is unnecessary, use catch { ... }: omit both the parameter and parentheses rather than writing catch (). Do not silently ignore errors without an intentional decision; an unhandled case can be propagated with throw error. An exception thrown inside catch is not caught again by that same clause.

If catch completes normally, execution continues after the entire statement only if there is no finally block or that block also completes normally. If present, finally runs before control leaves the statement and can still throw an exception, return from the function, or otherwise redirect control. In the example, throw skips adding "unreachable", and catch records the error message.

Examples

const events = [];

try {
  events.push("start");
  throw new Error("invalid value");
  events.push("unreachable");
} catch (error) {
  events.push(error.message);
}

events.push("done");
console.log(events.join(" | "));

Result

start | invalid value | done

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources