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
- The code inside
try { ... }starts running line by line. - If it doesn't raise any errors,
catchis not called. - If an error occurs inside
try, execution oftrystops immediately, and control moves tocatch. - Inside
catch(error)you have access to the error object, which you can handle or log.
Example 1. No error -> catch doesn't run
try {
console.log("Everything is working great!");
} catch (error) {
console.log("Caught an error:", error.message);
}
console.log("Continuing execution...");Output:
Everything is working great!
Continuing execution...Since there was no error inside
try,catchdoesn't run.
Example 2. There is an error -> catch runs
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:
Start
Caught: An error occurred!After
throw, execution oftrystops, and control passes tocatch.
Example 3. An automatic error
try {
console.log(a); // ReferenceError: a is not defined
} catch (error) {
console.log("Caught:", error.name); // ReferenceError
}Here
catchruns because JS automatically threw an exception when accessing an undefined variable.
Example 4. Several lines - an error "cuts off" try
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:
1
Caught: Unexpected token n in JSON at position 0
3As soon as the error happens,
trystops, andcatchruns.
Example 5. An error outside try - catch doesn't run
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 caughtThe error won't be caught, because it happened outside the try block.
Example 6. An error inside asynchronous code
Important:
try...catchdoes not catch errors inside callbacks and promises unless you useawaitor.catch().
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/awaitor.catch():
Correct:
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
try {
throw new Error("Error 1");
throw new Error("Error 2"); // will not run
} catch (err) {
console.log("Caught:", err.message);
}Output:
Caught: Error 1Summary
| Situation | Does catch run? | Reason |
|---|---|---|
No errors in try | No | The code ran normally |
throw in try | Yes | The exception was handled |
| JS itself threw an error (ReferenceError, etc.) | Yes | Automatic error |
Error outside try | No | Not within the handled scope |
| Error in a callback/Promise without await | No | Asynchronous code outside try |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.