Skip to solution
easyFrontend

How does `try...catch` work in JavaScript?

60 views
01

Understand the problem

Question presented to candidate: "Walk me through exactly how try, catch, and finally work in JavaScript -- including a case or two where the behavior might surprise someone."

What a strong answer should cover:

  • try wraps code that might throw; if anything inside it throws, control jumps straight to catch with the thrown value, skipping the rest of the try block entirely.
  • finally always runs -- on the success path, on the caught-error path, and even if try or catch contains a return statement.
  • A genuinely surprising, verified edge case: a return statement inside finally overrides whatever try or catch already returned.
  • try...catch only catches synchronous throws inside the try block, and a rejected promise that is explicitly awaited inside it -- it does NOT catch a throw from inside a setTimeout callback or an unawaited async function, since those run on a later turn, after the try block has already finished.
  • Since ES2019, catch can be written with no bound parameter (catch { ... }) when the error value itself is not needed.

Clarifying questions expected:

  • "Should I focus on synchronous error handling, or also cover how it interacts with async/await and promise rejections?"

Code / implementation expected: Yes -- a short, runnable snippet demonstrating a caught sync throw, finally always running, and an awaited rejection being caught.

error handlingtry/catchexceptionscontrol flow
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript error-handling interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JStry/catch/finally, optional catch binding, finally overriding a return, awaited rejection (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 41 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track