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) ਆਮ ਤੌਰ 'ਤੇ programming errors ਨੂੰ ਦਰਸਾਉਂਦੇ ਹਨ — ਤੁਸੀਂ bug ਨੂੰ ਠੀਕ ਕਰਦੇ ਹੋ ਹਰ ਥਾਂ ਉਸਨੂੰ ਫੜਨ ਦੀ ਬਜਾਏ। 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)
Mukh error handling ਜ਼ਰੂਰੀ ਹੈ, ਅਤੇ Java ਦਾ checked/unchecked distinction ਇੱਕ ਪ੍ਰਭਾਵਸ਼ਾਲੀ (ਅਤੇ ਬਹਸ ਕੀਤਾ) ਵਿਸ਼ੇਸ਼ਤਾ ਹੈ ਜੋ ਡੂੰਘਾਈ ਨਾਲ ਸਮਝਣ ਯੋਗ ਹੈ।
Checked exceptions ਸੰਭਾਵੀ ਬਾਹਰੀ ਅਸਫਲ (I/O, DB) ਦੀ ਸਪਸ਼ਟ ਹੈਂਡਲਿੰਗ ਨੂੰ ਮਜਬੂਰ ਕਰਦੇ ਹਨ — ਭਰੋਸੇਯੋਗਤਾ ਨੂੰ ਉਤਸ਼ਾਹਿਤ ਕਰਦੇ ਹਨ ਪਰ ਕਈ ਵਾਰ verbose ਹੋਣ ਦੀ ਆਲੋਚਨਾ ਕੀਤੀ ਜਾਂਦੀ ਹੈ; unchecked exceptions ਰੁਟੀਨ ਤੌਰ ਤੇ ਫੜਨ ਦੀ ਬਜਾਏ ਠੀਕ ਕਰਨ ਲਈ bugs ਨੂੰ ਦਰਸਾਉਂਦੇ ਹਨ।
Hierarchy ਨੂੰ ਜਾਣਨਾ, ਜਦੋਂ ਹਰੇਕ ਕਿਸਮ ਲਾਗੂ ਹੁੰਦਾ ਹੈ, try/catch/finally semantics, ਅਤੇ ਆਧੁਨਿਕ try-with-resources ਆਟੋ cleanup ਲਈ ਸਹੀ, ਭਰੋਸੇਯੋਗ Java ਲਿਖਣ ਲਈ ਬੁਨਿਆਦੀ ਹੈ — ਅਤੇ ਆਪਣੇ exceptions ਨੂੰ ਸਹੀ ਢੰਗ ਨਾਲ ਡਿਜ਼ਾਈਨ ਕਰਨ ਲਈ।
ਇਹ ਵਿਭਾਜਨ Java APIs ਕਿਵੇਂ ਡਿਜ਼ਾਈਨ ਕੀਤੇ ਗਏ ਹਨ ਨੂੰ ਪ੍ਰਭਾਵਿਤ ਕਰਦਾ ਹੈ ਅਤੇ ਇੱਕ ਵਾਰ-ਵਾਰ ਸਾਖਾਤਕਾਰ ਅਤੇ code-review ਵਿਸ਼ਾ ਹੈ।