← Back to JavaScript Course | Chapter 8: Error Handling | Lesson 2 of 6

JS try catch finally

try, catch, and finally work like a safety net: try something risky, catch the problem if it fails, and always finish with clean-up. The finally part runs either way.
Syntax
javascript
try {
  // risky code
} catch (error) {
  // handle error
} finally {
  // always runs
}

The try Block

Code placed inside a try block runs normally until (and unless) it throws; if nothing throws, the catch block is skipped entirely and execution continues after it.

उदाहरण: The try Block

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Print "This runs" to the console
  // Print "This runs" to the console
  console.log("This runs");
// Catch any error, bound to `e`
// Catch any error, bound to `e`
} catch (e) {
  // Print "Skipped, nothing threw" to the console
  // Print "Skipped, nothing threw" to the console
  console.log("Skipped, nothing threw");
}

The catch Block

The catch block receives the thrown value as its parameter and runs only when the try block throws, letting you inspect the error, decide how to respond, and optionally recover instead of letting it propagate further.

उदाहरण: The catch Block

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  null.name;
// Catch any error, bound to `error`
// Catch any error, bound to `error`
} catch (error) {
  // Print `"Caught:", error.message` to the console
  // Print `"Caught:", error.message` to the console
  console.log("Caught:", error.message);
}

The finally Block

The finally block runs after try/catch regardless of whether an error was thrown or caught, making it the right place for cleanup code like closing a file or resetting UI state.

उदाहरण: The finally Block

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Print "try" to the console
  // Print "try" to the console
  console.log("try");
// Runs no matter what, whether or not an error was thrown
// Runs no matter what, whether or not an error was thrown
} finally {
  // Print "finally always runs" to the console
  // Print "finally always runs" to the console
  console.log("finally always runs");
}

Return with try catch finally

A return inside try is deferred until after finally runs; if finally also returns a value, that finally return silently overrides the one from try — a subtle gotcha worth knowing.

उदाहरण: Return with try catch finally

javascript
function test() {
  try {
    return "from try";
  } finally {
    return "from finally"; // overrides the try's return
  }
}
console.log(test());

Nested try catch

try/catch blocks can be nested, letting an inner block handle a specific, recoverable failure while an outer block catches anything more serious that escapes the inner one.

उदाहरण: Nested try catch

javascript
// Try running this block; jump to `catch` if it throws
// Try running this block; jump to `catch` if it throws
try {
  // Try running this block; jump to `catch` if it throws
  // Try running this block; jump to `catch` if it throws
  try {
    // Throw a new `Error` with message "inner failure"
    // Throw a new `Error` with message "inner failure"
    throw new Error("inner failure");
  // Catch any error, bound to `inner`
  // Catch any error, bound to `inner`
  } catch (inner) {
    // Print `"Handled inner:", inner.message` to the console
    // Print `"Handled inner:", inner.message` to the console
    console.log("Handled inner:", inner.message);
  }
// Catch any error, bound to `outer`
// Catch any error, bound to `outer`
} catch (outer) {
  // Print `"Handled outer:", outer.message` to the console
  // Print `"Handled outer:", outer.message` to the console
  console.log("Handled outer:", outer.message);
}
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.