🧯
Exceptions

Advanced Exception Handling

Multi-catch & Custom Hierarchy
💡 Multi-catch एक ही security guard है जो अलग-अलग तरह के troublemakers (exceptions) को एक ही gate पर सम्भाल लेता है, अलग-अलग gate बनाने की ज़रूरत नहीं।

एक catch block में "|" से multiple exception types पकड़ सकते हो: catch (IOException | SQLException e)।

Custom exceptions का अपना hierarchy बना सकते हो — जैसे एक base AppException, और उससे extend करके specific PaymentFailedException — बड़े projects में errors को organize करता है।

try {
  riskyOperation();
} catch (IOException | SQLException e) {
  System.out.println("Handled: " + e.getMessage());
}

class AppException extends Exception {}
class PaymentFailedException extends AppException {}
🧯
Multi-catch एक ही security guard है जो अलग-अलग तरह के troublemakers (exceptions) को एक ही gate पर सम्भाल लेता है, अलग-अलग gate बनाने की ज़रूरत नहीं।
1 / 4
⚡ झट से Recap
  • catch (A | B e) = multi-catch
  • Custom exceptions अपना hierarchy बना सकते हैं
  • बड़े projects में errors organize करता है
इस page में (4 subtopics)

कोई भी class जो AutoCloseable interface implement करती है (FileReader, Scanner, database Connection, आदि) try-with-resources में use हो सकती है। try(...) के अंदर resource declare करो, और block खत्म होते ही (चाहे normally या exception से) close() automatically call हो जाता है — manually finally में close() लिखने की ज़रूरत खत्म।

Java 9+ में resource को try() के बाहर भी declare कर सकते हो (अगर वो effectively final हो) और सिर्फ नाम try() में लिख सकते हो — थोड़ा cleaner syntax।

try (Scanner sc = new Scanner(System.in)) {
  String input = sc.nextLine();
} // sc.close() automatically ho gaya

// Multiple resources bhi ek saath:
try (FileReader fr = new FileReader("a.txt"); FileWriter fw = new FileWriter("b.txt")) {
  // dono automatically close honge, reverse order mein
}

Custom exception बनाते वक्त: meaningful नाम दो (InsufficientBalanceException, न कि MyException या Exception1), Exception या RuntimeException extend करो (checked vs unchecked decide करके — इस subtopic के last point में detail है), और एक constructor दो जो message accept करे और super(message) call करे।

Extra fields भी add कर सकते हो custom exception में — जैसे InsufficientBalanceException में "shortfallAmount" field, ताकि catch करने वाला code उस extra info को use कर सके।

class InsufficientBalanceException extends RuntimeException {
  final double shortfall;
  InsufficientBalanceException(String message, double shortfall) {
    super(message);
    this.shortfall = shortfall;
  }
}

कभी-कभी एक exception दूसरे exception की वजह से आती है — "cause" के through original exception को wrap कर सकते हो, ताकि original root-cause की info खोए नहीं (stack trace में "Caused by:" दिखता है)। new CustomException("msg", originalException) जैसा pattern — दूसरा argument "cause" है।

try {
  readFile();
} catch (IOException e) {
  throw new ServiceException("File load fail hua", e); // cause preserved
}

Checked exception (जो Exception extend करे, RuntimeException नहीं) use करो जब caller को *ज़रूर* handle करना चाहिए, और recovery possible हो (जैसे file not found — retry या alternate path try कर सकते हो)। Compiler force करता है कि या तो try-catch करो, या throws से आगे pass करो।

Unchecked (RuntimeException) use करो programming errors के लिए जो bugs हैं, recovery normally नहीं होती (जैसे invalid argument pass करना, null pointer) — इन्हें हर जगह try-catch करना मजबूरी नहीं है, compiler force नहीं करता।

CheckedUnchecked (RuntimeException)
Compiler force करता है handle करना?हाँनहीं
ExampleIOException, SQLExceptionNullPointerException, IllegalArgumentException
कब use करेंRecoverable, external problemsProgramming bugs, logic errors