Java try/catch/finally सह त्रुटी हाताळतो आणि checked अपवाद (घोषित किंवा हाताळले पाहिजे) unchecked अपवाद (runtime, लागू नाहीत) पासून अनन्यपणे वेगळे करतो. या भेदाचा अर्थ समजून घेणे Java त्रुटी हाताळणीचे केंद्रीय आहे.
Java try/catch/finally सह त्रुटी हाताळतो आणि checked अपवाद (घोषित किंवा हाताळले पाहिजे) unchecked अपवाद (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 नेहमी कार्यान्वित होते (स्वच्छता), जरी अपवाद फेकला जाए किंवा 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 अपवाद (IOException, SQLException) पुनरुद्धार करण्यायोग्य, अपेक्षित परिस्थिती दर्शवतात (फाइल गायब, नेटवर्क अपयश). कंपाइलर तुम्हाला त्यांना हाताळण्यास किंवा घोषित करण्यास बाध्य करतो — बाह्य-अपयश हाताळणी स्पष्ट करते.
// 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 उपवर्ग) सामान्यतः प्रोग्रामिंग त्रुटी दर्शवतात — तुम्ही बग दूर करता, ते सर्वत्र पकडत नाही. कंपाइलर हाताळणी लागू करत नाही.
// 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 अपवाद अपेक्षित बाह्य अपयशांची स्पष्ट हाताळणी (I/O, DB) लागू करतात — विश्वासार्हता वाढवतात पण काहीवेळा verbose म्हणून टीका केली जाते; unchecked अपवाद सर्वत्र नियमितपणे पकडण्याऐवजी दूर करण्यायोग्य बग दर्शवतात.
पदानुक्रम जाणून, प्रत्येक प्रकार कधी लागू होतो, try/catch/finally सिमेंटिक्स आणि आधुनिक try-with-resources स्वयंचलित स्वच्छतेसाठी हे मूलभूत आहे सही, विश्वसनीय Java लिहिणे — आणि तुमचे स्वतःचे अपवाद योग्यरीत्या डिজाईन करणे.
भेद Java APIs कसे डिজाईन केले जातात याच आकार देतो आणि ते साक्षात्कार आणि कोड-पुनरावलोकन विषय आहेत.