JavaScript try catch: Handle Errors and Exceptions

Learn JavaScript try catch with syntax, thrown errors, finally blocks, return vs throw behavior, and practical error-handling examples.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

JavaScript try catch: Handle Errors and Exceptions

try...catch is the standard JavaScript error-handling structure. Put code that might fail inside try, handle the failure in catch, and optionally run cleanup code in finally.

Use try...catch for runtime errors, validation failures that throw exceptions, JSON parsing, and async/await error handling. It is also the right tool to pair with Promise.reject() or parse flows such as JSON.parse().

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


Quick reference

Piece Role
try { } Run code that might throw
catch (err) { } Handle thrown values
finally { } Always runs after try / catch

Method 1: Catch a thrown error

javascript
try {
  throw new ReferenceError("x is not defined");
} catch (error) {
  console.log("try-catch:", error.name + ":" + error.message);
}
Output

You should see one line logging try-catch: ReferenceError:x is not defined.

When the error is thrown, control moves straight to catch.


Method 2: Use finally for Cleanup

javascript
try {
  console.log("work");
} catch (error) {
  console.log("error");
} finally {
  console.log("cleanup");
}
Output

You should see 2 lines, in order: work, cleanup.

finally runs whether the try block succeeds or fails.


Method 3: Return a fallback from catch

javascript
function safeParse(value) {
  try {
    return JSON.parse(value);
  } catch (error) {
    return null;
  }
}

console.log("ok:", safeParse('{"a":1}'));
console.log("bad:", safeParse("not json"));
Output

You should see two lines: ok: { a: 1 } (object may print in Node’s default { a: 1 } form), then bad: null.

Use a return value when the failure is expected and the caller can handle a fallback.


Method 4: try / catch with async / await

Rejected promises from await behave like thrown values inside the async function body.

javascript
async function load() {
  try {
    await Promise.reject(new Error("Async failed"));
  } catch (error) {
    console.log("async-caught:", error.message);
  }
}

load();
Output

You should see one line logging async-caught: Async failed.

This is the standard way to handle rejected promises inside async functions.


Common Questions About JavaScript try catch

What does try catch do in JavaScript?

It catches runtime exceptions so code can recover or fail gracefully instead of crashing immediately.

Does try catch catch syntax errors?

No. Syntax errors happen before the code runs, so they are not caught by a normal try...catch around already-parsed code.

What is the difference between throw and return in error handling?

throw raises an exception. return sends a normal value back to the caller.

When should I use try catch around JSON.parse?

Use it when the input might be invalid JSON and you want to recover gracefully instead of crashing the request or UI flow.


Summary

Use JavaScript try...catch to handle exceptions in synchronous code and in async/await flows. Put risky code inside try, handle the failure in catch, and use finally for cleanup. Return fallback values only when the failure is expected and should not be treated as an exception, and use it around parsing or promise rejection paths when the input is not guaranteed to be valid.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …