JS try catch finally
In this page:
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
// 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
// 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
// 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
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
// 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);
}
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: