Skip to main content

When does the catch block run?

Short answer

The catch block runs only when an error (exception) occurs in the try block, either thrown manually (throw) or automatically (for example, ReferenceError, TypeError, and so on).

If the code inside try runs without errors, catch is skipped.


Step-by-step mechanism

  1. The code inside try { ... } starts running line by line.
  2. If it doesn't raise any errors, catch is not called.
  3. If an error occurs inside try, execution of try stops immediately, and control moves to catch.
  4. Inside catch(error) you have access to the error object, which you can handle or log.

Example 1. No error -> catch doesn't run

javascript
try { console.log("Everything is working great!"); } catch (error) { console.log("Caught an error:", error.message); } console.log("Continuing execution...");

Output:

javascript
Everything is working great! Continuing execution...

Since there was no error inside try, catch doesn't run.


Example 2. There is an error -> catch runs

javascript
try { console.log("Start"); throw new Error("An error occurred!"); console.log("This code will not run"); } catch (error) { console.log("Caught:", error.message); }

Output:

javascript
Start Caught: An error occurred!

After throw, execution of try stops, and control passes to catch.


Example 3. An automatic error

javascript
try { console.log(a); // ReferenceError: a is not defined } catch (error) { console.log("Caught:", error.name); // ReferenceError }

Here catch runs because JS automatically threw an exception when accessing an undefined variable.


Example 4. Several lines - an error "cuts off" try

javascript
try { console.log("1"); JSON.parse("not valid JSON"); // error here console.log("2"); // will not run } catch (error) { console.log("Caught:", error.message); } console.log("3");

Output:

javascript
1 Caught: Unexpected token n in JSON at position 0 3

As soon as the error happens, try stops, and catch runs.


Example 5. An error outside try - catch doesn't run

javascript
try { console.log("Everything is fine inside try"); } catch (error) { console.log("Caught an error:", error.message); } throw new Error("Error outside try!"); // won't be caught

The error won't be caught, because it happened outside the try block.


Example 6. An error inside asynchronous code

Important: try...catch does not catch errors inside callbacks and promises unless you use await or .catch().

javascript
try { setTimeout(() => { throw new Error("Error in the callback"); // won't be caught }, 1000); } catch (e) { console.log("Caught:", e.message); }

To catch errors from promises, you need to use async/await or .catch():

Correct:

javascript
async function run() { try { await Promise.reject(new Error("Error in the promise")); } catch (e) { console.log("Caught:", e.message); } } run();

Example 7. If there are several errors, the first one is caught

javascript
try { throw new Error("Error 1"); throw new Error("Error 2"); // will not run } catch (err) { console.log("Caught:", err.message); }

Output:

javascript
Caught: Error 1

Summary

SituationDoes catch run?Reason
No errors in tryNoThe code ran normally
throw in tryYesThe exception was handled
JS itself threw an error (ReferenceError, etc.)YesAutomatic error
Error outside tryNoNot within the handled scope
Error in a callback/Promise without awaitNoAsynchronous code outside try

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.