يتعامل Java مع الأخطاء باستخدام try/catch/finally، ويميز بشكل فريد بين الاستثناءات المفحوصة (يجب التصريح عنها أو معالجتها) والاستثناءات غير المفحوصة (وقت التشغيل، غير مفروضة). فهم هذا التمييز أمر أساسي في معالجة الأخطاء في Java.
يتعامل Java مع الأخطاء باستخدام try/catch/finally، ويميز بشكل فريد بين الاستثناءات المفحوصة (يجب التصريح عنها أو معالجتها) والاستثناءات غير المفحوصة (وقت التشغيل، غير مفروضة). فهم هذا التمييز أمر أساسي في معالجة الأخطاء في 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
}
الاستثناءات المفحوصة (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)
الاستثناءات غير المفحوصة (فئات فرعية من 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 هو ميزة أساسية (وموضع نقاش) تستحق الفهم العميق.
الاستثناءات المفحوصة تفرض معالجة صريحة للأخطاء الخارجية المتوقعة (I/O، DB) — مما يعزز الموثوقية لكن ينتقد أحياناً لأنه مفصول؛ الاستثناءات غير المفحوصة تمثل أخطاء يجب إصلاحها بدلاً من اصطيادها روتينياً.
معرفة التسلسل، ومتى يتم تطبيق كل نوع، وفهم دلالات try/catch/finally، وtry-with-resources الحديث للتنظيف التلقائي أمر أساسي لكتابة Java صحيح وموثوق — وللتصميم المناسب لاستثناءاتك.
هذا التمييز يشكل كيفية تصميم واجهات برمجة التطبيقات في Java وهو موضوع متكرر في المقابلات ومراجعات الكود.