Java ले try/catch/finally को साथ errors ह्यान्डल गर्छ, र अद्वितीय रूपमा checked exceptions (घोषणा वा ह्यान्डल गर्नुपर्छ) लाई unchecked exceptions (runtime, लागु गरिएको छैन) बाट अलग गर्छ। यो भेद Java error handling को लागि केन्द्रीय छ।
Java ले try/catch/finally को साथ errors ह्यान्डल गर्छ, र अद्वितीय रूपमा checked exceptions (घोषणा वा ह्यान्डल गर्नुपर्छ) लाई unchecked exceptions (runtime, लागु गरिएको छैन) बाट अलग गर्छ। यो भेद Java error handling को लागि केन्द्रीय छ।
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 सधैं कार्य गर्छ (cleanup), भले पनि एक 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 subclasses) सामान्यतः प्रोग्रामिङ त्रुटिहरू सङ्केत गर्छन् — तपाई बग ठीक गर्नुहुन्छ बजाय यसलाई सर्वत्र क्याच गर्नु। 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)
दृढ error handling आवश्यक छ, र Java को checked/unchecked distinction एक परिभाषित (र बहस गरिएको) विशेषता छ जो गहिराईसँग बुझ्न योग्य छ।
Checked exceptions ले अपेक्षित बाह्य असफलताहरू (I/O, DB) को स्पष्ट ह्यान्डलिङ लागु गर्छन् — विश्वसनीयता प्रवर्द्धन गर्छन् तर कहिलेकाहीं verbose को रूपमा आलोचनासम्म; unchecked exceptions ले नियमित रूपमा क्याच गर्नु भन्दा ठीक गर्नको लागि बग प्रतिनिधित्व गर्छन्।
Hierarchy, कहिले प्रत्येक प्रकार लागु हुन्छ, try/catch/finally semantics, र आधुनिक try-with-resources स्वचालित cleanup को लागि जानु सही, विश्वस्त Java लेखनको लागि आधारभूत छ — र तपाईंको आफ्नै exceptions उपयुक्त रूपमा डिजाइन गर्न।
यो भेद Java APIs कसरी डिजाइन गरिएको छ भनेर आकार दिन्छ र साक्षात्कार र code-review को विषय हो।