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

JS का try catch finally

try, catch, और finally एक safety net की तरह काम करते हैं: कोई risky चीज़ try करें, fail होने पर समस्या catch करें, और हमेशा clean-up के साथ खत्म करें। finally हिस्सा दोनों ही स्थिति में चलता है।
Syntax
javascript
try {
  // risky code
} catch (error) {
  // handle error
} finally {
  // always runs
}

try Block

try block के अंदर रखा गया code तब तक सामान्य रूप से चलता है जब तक (और जब तक) वह throw न करे; अगर कुछ भी throw न हो, तो catch block पूरी तरह skip हो जाता है और execution उसके बाद जारी रहता है।

उदाहरण: 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");
}

catch Block

catch block को thrown value अपने parameter के रूप में मिलती है और यह सिर्फ तभी चलता है जब try block throw करे, जो आपको error को देखने, यह तय करने कि कैसे respond करें, और चाहें तो इसे आगे propagate होने देने के बजाय recover करने देता है।

उदाहरण: 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);
}

finally Block

finally block try/catch के बाद चलता है चाहे कोई error thrown या caught हुआ हो या नहीं, जो इसे किसी file को बंद करने या UI state reset करने जैसे cleanup code के लिए सही जगह बनाता है।

उदाहरण: 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");
}

try catch finally के साथ Return

try के अंदर एक return तब तक टलता रहता है जब तक finally न चल जाए; अगर finally भी एक value return करे, तो वह finally return चुपचाप try वाले return को override कर देता है — यह जानने लायक एक बारीक gotcha है।

उदाहरण: 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 को nest किया जा सकता है, जिससे एक inner block किसी खास, recoverable failure को संभाल सकता है जबकि एक outer block उससे ज़्यादा गंभीर किसी भी चीज़ को catch करता है जो inner block से बच निकले।

उदाहरण: 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. #}
आम गलतियां
  1. यह उम्मीद करना कि try...catch setTimeout जैसे asynchronous callbacks से errors catch करेगा, जबकि वे try block के खत्म होने के बाद चलते हैं।
  2. finally में एक value return करना, जो try से return या throw को override कर देता है।
  3. error को inspect किए बिना catch इस्तेमाल करना, इसलिए एक TypeError को एक expected error जैसा ही माना जाता है।
🔒

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.