انتقل إلى المحتوى
أمين أبوزيد
BEG-029

الاستثناءات (Exceptions)

  • 8 درس
  • 95 دقيقة قراءة
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • المفحوصة مقابل غير المفحوصة (Checked vs unchecked)
  • try/catch/finally
  • throw/throws
  • الاستثناءات المخصصة
  • try-with-resources

تعلّم أولًا

95 دقيقة قراءة · 4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Distinguish checked exceptions, unchecked exceptions, and errors, and explain the hierarchy.
  2. Handle exceptions with try/catch/finally and multi-catch, catching the narrowest useful type.
  3. Throw exceptions with meaningful messages and declare them with throws.
  4. Create a custom exception for a business rule violation.
  5. Release resources with try-with-resources.

الدروس

  1. 01ما الذي يسوء أثناء التشغيل12 د قراءة
  2. 02هرمية الاستثناءات12 د قراءة
  3. 03try وcatch وfinally12 د قراءة
  4. 04الإلقاء والتصريح12 د قراءة
  5. 05الاستثناءات المخصصة12 د قراءة
  6. 06try-with-resources12 د قراءة
  7. 07عرض عملي: InsufficientFundsException من البداية إلى النهاية11 د قراءة
  8. 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)

IDOutcomeTask
BEG-029-EX011Classify as checked, unchecked, or error: IOException, NullPointerException, OutOfMemoryError, IllegalArgumentException.
BEG-029-EX022Write code that parses a string to int and prints a friendly message on failure, plus a finally that prints "done".
BEG-029-EX032Rewrite two identical catch blocks for NumberFormatException and ArithmeticException as one multi-catch.
BEG-029-EX043, 4Write OutOfStockException (with a cause constructor) and a method that throws it with a message naming the item and quantity.
BEG-029-EX055Rewrite a BufferedReader read with manual finally { close(); } as try-with-resources.
BEG-029-EX062Review: 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);} } then throw 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 rethrow new 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which is a checked exception?O1
  2. 2NullPointerException is:O1
  3. 3Which should normal code not try to catch?O1
  4. 4When does finally run?O2
  5. 5With two catch blocks, the more specific type should come:O2
  6. 6The best catch is:O2
  7. 7throws IOException on a method means:O3
  8. 8A good exception message:O3
  9. 9A custom checked exception extends:O4
  10. 10try-with-resources requires the resource to implement:O5

الجزء العملي

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).

اكتب للبحث في المستويات والمراحل والوحدات والدروس.