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) પુનrાવર્તનશીલ, અપેક્ષિત શરતોનું પ્રતિનિધિત્વ કરે છે (ફાઇલ ખોવાયેલી, નેટવર્ક નિષ્ફળતા). 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) સામાન્યતઃ પ્રોગ્રામિંગ ભૂલોનો સંકેત આપે છે — તમે તેને દર જગ્યાએ catch કરવાને બદલે બગ ઠીક કરો. 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) ના સ્પષ્ટ સંચાલનને બાધ્ય કરે છે — વિશ્વસનીયતા સુધારે છે પરંતુ કેટલીક વાર વર્બોસ તરીકે ટીકા કરવામાં આવે છે; unchecked exceptions બગ્સનું પ્રતિનિધિત્વ કરે છે જેને નિયમિત પ્રમાણે catch કરવાને બદલે ઠીક કરવું જોઈએ.
શ્રેણીક્રમ જાણવું, દરેક પ્રકાર ક્યારે લાગુ હોય, try/catch/finally semantic અને આધુનિક try-with-resources સ્વચાલિત સાફ સફાઇ માટે સાચા, વિશ્વાસપાત્ર Java લખવા અને તમારા પોતાના exceptions યોગ્ય રીતે ડિજાઇન કરવા માટે મૌલિક છે.
તફાવત Java APIs કેવી રીતે ડિજાઇન કરવામાં આવે છે તે આકાર આપે છે અને તે ઘણીવાર interview અને code-review વિષય છે.