الاستثناءات (Exceptions)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- المفحوصة مقابل غير المفحوصة (Checked vs unchecked)
- try/catch/finally
- throw/throws
- الاستثناءات المخصصة
- try-with-resources
تعلّم أولًا
95 دقيقة قراءة · 4 ساعة تدريب (تقدير)
مخرجات التعلم
- Distinguish checked exceptions, unchecked exceptions, and errors, and explain the hierarchy.
- Handle exceptions with
try/catch/finallyand multi-catch, catching the narrowest useful type. - Throw exceptions with meaningful messages and declare them with
throws. - Create a custom exception for a business rule violation.
- Release resources with try-with-resources.
الدروس
- 01ما الذي يسوء أثناء التشغيل12 د قراءة
- 02هرمية الاستثناءات12 د قراءة
- 03try وcatch وfinally12 د قراءة
- 04الإلقاء والتصريح12 د قراءة
- 05الاستثناءات المخصصة12 د قراءة
- 06try-with-resources12 د قراءة
- 07عرض عملي: InsufficientFundsException من البداية إلى النهاية11 د قراءة
- 08عرض عملي: العثور على استثناء مبتلَع12 د قراءة
المختبر والتمارين
Lab
BEG-029-LAB1 · Bank with rules
Outcomes: 1–5 · Time: ~2 h Build an Account with deposit and withdraw. Overdrawing throws InsufficientFundsException. A console loop reads amounts as text and retries when NumberFormatException occurs. Read one small file using try-with-resources. Acceptance: no empty catch blocks; messages helpful to the user.
Exercises (assessment: five exercises plus a "fix the swallowed exception" review)
| ID | Outcome | Task |
|---|---|---|
| BEG-029-EX01 | 1 | Classify as checked, unchecked, or error: IOException, NullPointerException, OutOfMemoryError, IllegalArgumentException. |
| BEG-029-EX02 | 2 | Write code that parses a string to int and prints a friendly message on failure, plus a finally that prints "done". |
| BEG-029-EX03 | 2 | Rewrite two identical catch blocks for NumberFormatException and ArithmeticException as one multi-catch. |
| BEG-029-EX04 | 3, 4 | Write OutOfStockException (with a cause constructor) and a method that throws it with a message naming the item and quantity. |
| BEG-029-EX05 | 5 | Rewrite a BufferedReader read with manual finally { close(); } as try-with-resources. |
| BEG-029-EX06 | 2 | Review: fix try { load(); } catch (Exception e) {} so the error is not swallowed. |
أظهر الحلول
- EX01 Checked, unchecked, error, unchecked.
- EX02
try { int n = Integer.parseInt(s); } catch (NumberFormatException e) { System.out.println("'" + s + "' is not a whole number."); } finally { System.out.println("done"); } - EX03
catch (NumberFormatException | ArithmeticException e) { ... }. - EX04
class OutOfStockException extends Exception { OutOfStockException(String m){super(m);} OutOfStockException(String m, Throwable c){super(m,c);} }thenthrow new OutOfStockException("Only 2 of 'pen' left, requested 5"); - EX05
try (BufferedReader r = Files.newBufferedReader(p)) { ... } catch (IOException e) { ... }. - EX06 Catch the narrow type (for example
IOException), print a clear message, and either recover or rethrownew IllegalStateException("Could not load data", e). - Outcome 1 is also covered by quiz Q01–Q03; outcome 3 is also covered by the lab.
الاختبار
Outcomes: O1 hierarchy · O2 try/catch/finally · O3 throw and throws · O4 custom exceptions · O5 try-with-resources.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Five exercises BEG-029-EX01 to EX05 plus the swallowed-exception review EX06 and the lab. Pass: at least four exercises correct, the review fixed, and no empty catch in the lab.
أخطاء شائعة
catch (Exception e) {}; using exceptions for normal control flow; losing the original cause.
التقييم
5 exercises plus a "fix the swallowed exception" review.
الصلة بالمسار الوظيفي
API error handling (JNR-015) and transaction rollback rules (JNR-031).