JS का try catch finally
try {
// risky code
} catch (error) {
// handle error
} finally {
// always runs
}
try Block
try block के अंदर रखा गया code तब तक सामान्य रूप से चलता है जब तक (और जब तक) वह throw न करे; अगर कुछ भी throw न हो, तो catch block पूरी तरह skip हो जाता है और execution उसके बाद जारी रहता है।
उदाहरण: 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");
}
catch Block
catch block को thrown value अपने parameter के रूप में मिलती है और यह सिर्फ तभी चलता है जब try block throw करे, जो आपको error को देखने, यह तय करने कि कैसे respond करें, और चाहें तो इसे आगे propagate होने देने के बजाय recover करने देता है।
उदाहरण: 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);
}
finally Block
finally block try/catch के बाद चलता है चाहे कोई error thrown या caught हुआ हो या नहीं, जो इसे किसी file को बंद करने या UI state reset करने जैसे cleanup code के लिए सही जगह बनाता है।
उदाहरण: 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");
}
try catch finally के साथ Return
try के अंदर एक return तब तक टलता रहता है जब तक finally न चल जाए; अगर finally भी एक value return करे, तो वह finally return चुपचाप try वाले return को override कर देता है — यह जानने लायक एक बारीक gotcha है।
उदाहरण: 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 को nest किया जा सकता है, जिससे एक inner block किसी खास, recoverable failure को संभाल सकता है जबकि एक outer block उससे ज़्यादा गंभीर किसी भी चीज़ को catch करता है जो inner block से बच निकले।
उदाहरण: 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);
}
- यह उम्मीद करना कि
try...catchsetTimeoutजैसे asynchronous callbacks से errors catch करेगा, जबकि वेtryblock के खत्म होने के बाद चलते हैं। finallyमें एक value return करना, जोtryसेreturnयाthrowको override कर देता है।- error को inspect किए बिना
catchइस्तेमाल करना, इसलिए एकTypeErrorको एक expected error जैसा ही माना जाता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: