100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

try-catch in JavaScript

The mechanics of the try, catch, and finally blocks, including execution order and how finally interacts with return statements.

Error Handling & Interview PrepBeginner10 min readJul 8, 2026
Analogies

1. Introduction

The try...catch...finally statement is JavaScript's core construct for handling exceptions locally instead of letting them crash the program. Code that might fail goes inside try; recovery logic goes inside catch; and cleanup logic that must run regardless of success or failure goes inside finally.

🏏

Cricket analogy: try-catch-finally is like attempting a risky reverse sweep in try, having a recovery plan in catch if you're bowled out, and walking back to the pavilion in finally regardless of whether the shot succeeded or failed.

2. Syntax

javascript
try {
  // code that may throw
} catch (error) {
  // handle error
} finally {
  // always runs
}

// catch binding is optional (ES2019+)
try {
  riskyOperation();
} catch {
  console.log("Something went wrong");
}

3. Explanation

Execution enters the try block first. If no error is thrown, the catch block is skipped entirely and control moves to finally (if present). If an error is thrown anywhere inside try — including inside a function it calls — execution jumps immediately to catch, with the thrown value bound to the catch parameter. After catch finishes (or if there was no error), finally runs last, before control leaves the whole statement.

🏏

Cricket analogy: If the reverse sweep in try goes clean, execution skips catch and heads straight to walking off in finally; if it's caught out anywhere in the shot — even mid-swing — play jumps to catch with the dismissal recorded, then finally still runs, sending the batsman off regardless.

Since ES2019, the catch parameter is optional: catch { ... } is valid when you don't need to inspect the error object itself, just that something failed.

🏏

Cricket analogy: Since a rule change, a fielder can simply signal 'dropped catch' in catch { } without needing to log exactly which finger it slipped off — sometimes you just need to know something went wrong, not the full detail.

finally always runs, even if try or catch contains a return, throw, break, or continue. If finally itself contains a return, it overrides any return value from try or catch — this is a common source of subtle bugs, so avoid returning from finally unless intentional.

4. Example

javascript
function testFinally() {
  try {
    console.log("try block");
    return "from try";
  } catch (err) {
    console.log("catch block");
    return "from catch";
  } finally {
    console.log("finally block");
  }
}

console.log("Result:", testFinally());

try {
  JSON.parse("{ invalid json }");
} catch (err) {
  console.log("Parse failed:", err.name);
} finally {
  console.log("Parsing attempt complete");
}

5. Output

text
try block
finally block
Result: from try
Parse failed: SyntaxError
Parsing attempt complete

6. Key Takeaways

  • try runs first; catch only runs if an error is thrown inside try.
  • finally always executes — on success, on error, and even when try/catch contain return.
  • The catch parameter is optional since ES2019: catch { ... }.
  • A return inside finally silently overrides a return from try or catch — avoid it unless intentional.
  • try-catch only handles synchronous errors in the block it wraps, not errors from callbacks scheduled later.

Practice what you learned

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#TryCatchInJavaScript#Try#Catch#Syntax#Explanation#ErrorHandling#StudyNotes#SkillVeris