Java try/catch/finally के साथ त्रुटियों को संभालता है, और checked exceptions (जिन्हें घोषित या संभाला जाना चाहिए) से unchecked exceptions (runtime, लागू नहीं) का अनोखा अंतर करता है। यह अंतर Java में त्रुटि संभालने के लिए केंद्रीय है।
Java try/catch/finally के साथ त्रुटियों को संभालता है, और checked exceptions (जिन्हें घोषित या संभाला जाना चाहिए) से unchecked exceptions (runtime, लागू नहीं) का अनोखा अंतर करता है। यह अंतर Java में त्रुटि संभालने के लिए केंद्रीय है।
try {
FileReader f = new FileReader("file.txt"); // may throw IOException
// ... use it ...
} catch (FileNotFoundException e) {
System.out.println("not found: " + e.getMessage()); // specific first
} catch (IOException e) {
System.out.println("io error"); // broader after
} finally {
System.out.println("always runs — cleanup"); // runs no matter what
}
finally हमेशा निष्पादित होता है (सफाई), भले ही exception फेंका जाए या return आए।
Throwable
├── Error — serious JVM problems (OutOfMemoryError) — don't catch
└── Exception
├── RuntimeException → UNCHECKED (not enforced by the compiler)
│ NullPointerException, IllegalArgumentException, IndexOutOfBounds...
└── (other Exceptions) → CHECKED (compiler enforces handling)
IOException, SQLException...
// MUST either catch it or declare `throws` — the compiler won't let you ignore it
public void readFile() throws IOException { // declare it
Files.readAllLines(Paths.get("file.txt")); // throws checked IOException
}
Checked exceptions (IOException, SQLException) पुनर्प्राप्त करने योग्य, अपेक्षित स्थितियों का प्रतिनिधित्व करते हैं (फाइल गायब, नेटवर्क विफलता)। Compiler आपको उन्हें संभालने या घोषित करने के लिए बाध्य करता है — बाहरी विफलता संभालना स्पष्ट बनाता है।
// NOT required to declare or catch — usually indicate PROGRAMMING BUGS
String s = null;
s.length(); // NullPointerException (unchecked)
int x = arr[10]; // ArrayIndexOutOfBoundsException (unchecked)
Integer.parseInt("abc"); // NumberFormatException (unchecked)
Unchecked (RuntimeException उपवर्ग) आमतौर पर प्रोग्रामिंग त्रुटियों का संकेत देते हैं — आप इसे हर जगह पकड़ने के बजाय बग को ठीक करते हैं। Compiler संभालने को बाध्य नहीं करता।
// resources implementing AutoCloseable are closed automatically
try (FileReader f = new FileReader("file.txt")) {
// use f
} // f.close() called automatically, even on exception — no finally needed
✓ Catch specific exceptions, not bare Exception
✓ Use try-with-resources for files/connections (auto-close)
✓ Don't swallow exceptions silently (empty catch blocks)
✓ Throw meaningful exceptions; include context in messages
✓ Custom exceptions extend Exception (checked) or RuntimeException (unchecked)
मजबूत त्रुटि संभालना आवश्यक है, और Java का checked/unchecked अंतर एक परिभाषित (और विवादास्पद) विशेषता है जिसे गहराई से समझना चाहिए।
Checked exceptions अपेक्षित बाहरी विफलताओं (I/O, DB) के स्पष्ट संभालने को बाध्य करते हैं — विश्वसनीयता को बढ़ावा देते हैं लेकिन कभी-कभी verbose के रूप में आलोचना की जाती है; unchecked exceptions बग का प्रतिनिधित्व करते हैं जिन्हें नियमित रूप से पकड़ने के बजाय ठीक करना चाहिए।
पदानुक्रम जानना, प्रत्येक प्रकार कब लागू होता है, try/catch/finally शब्दार्थ, और आधुनिक try-with-resources स्वचालित सफाई के लिए सही, विश्वसनीय Java लिखने और अपने exceptions को उचित रूप से डिज़ाइन करने के लिए मौलिक है।
अंतर Java APIs को कैसे डिज़ाइन किया जाता है यह आकार देता है और साक्षात्कार और code-review विषय है।