מפגש 12: מה זו חריגה, try, catch ו-finally, זריקה עם throw, והודעות שגיאה שעוזרות.Session 12: What an exception is, try, catch and finally, throwing with throw, and error messages that help.
מה זו חריגה, try, catch ו-finally, זריקה עם throw, והודעות שגיאה שעוזרות. בסוף המפגש מוסיפים צעד לפרויקט המלווה.What an exception is, try, catch and finally, throwing with throw, and error messages that help. At the end we add a step to the companion project.
מה נעשה היוםToday's plan
מונחיםTerms
חריגה שאף אחד לא תופסAn exception nobody catches
try ו-catch: ממשיכים לרוץtry and catch: keep running
finally רץ תמידfinally always runs
throw: אתם מחליטים מה לא חוקיthrow: you decide what's illegal
טעויות נפוצותCommon mistakes
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.
נושאTopic 1
מונחיםTerms
המונחים של המפגשThis session's terms
try ו-catch: `try { ...try and catch: `try { ...
finally: בלוק שרץ אחרי try/catch תמיד:finally: A block that runs after try/catch always:
throw: זורק חריגה בעצמכם:throw: Throws an exception yourself:
NumberFormatException: נזרקת כש-Integer.parseInt או Double.parseDouble מקבלות טקסט שאינו מספר:NumberFormatException: Thrown when Integer.parseInt or Double.parseDouble get text that isn't a number:
IllegalArgumentException: החריגה המקובלת כשמתודה מקבלת ערך לא חוקי.IllegalArgumentException: The usual exception when a method gets an illegal value.
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.
אף אחד לא תופס אותה, ולכן התוכנית נעצרת, Java מדפיסה stack trace, והשורה האחרונה לא רצה.Nobody catches it, so the program stops, Java prints a stack trace, and the last line never runs.
Integer.parseInt("12a") זורקת NumberFormatException. אף אחד לא תופס אותה, ולכן התוכנית נעצרת, Java מדפיסה stack trace, והשורה האחרונה לא רצה.Integer.parseInt("12a") throws a NumberFormatException. Nobody catches it, so the program stops, Java prints a stack trace, and the last line never runs.
טעות נפוצהCommon mistake
Main.java · main
System.out.println("Start");
int n =Integer.parseInt("12a");
System.out.println(n *2);
מה Java עונהWhat Java says
Exception in thread "main" java.lang.NumberFormatException: For input string: "12a"
NumberFormatException: For input string: "12a" בשורה 4. Start כבר הודפס, אבל n * 2 לא.NumberFormatException: For input string: "12a" on line 4. Start was already printed, but n * 2 never is.
חריגה שאף אחד לא תופסAn exception nobody catches
נושאTopic 3
try ו-catch: ממשיכים לרוץtry and catch: keep running
try ו-catch: ממשיכים לרוץtry and catch: keep running
עוטפים את מה שעלול להיכשל ב-try.Wrap what might fail in a try.
כשנזרקת חריגה, Java קופצת ל-catch המתאים, ואחריו התוכנית ממשיכה.When an exception is thrown, Java jumps to the matching catch, and then the program goes on.
בסימולציה רואים את הקפיצה: השורה שאחרי parseInt לא רצה בפעם השנייה.In the simulation you see the jump: the line after parseInt doesn't run the second time.
עוטפים את מה שעלול להיכשל ב-try. כשנזרקת חריגה, Java קופצת ל-catch המתאים, ואחריו התוכנית ממשיכה. בסימולציה רואים את הקפיצה: השורה שאחרי parseInt לא רצה בפעם השנייה.Wrap what might fail in a try. When an exception is thrown, Java jumps to the matching catch, and then the program goes on. In the simulation you see the jump: the line after parseInt doesn't run the second time.
השורה הבאה ב-try מדולגת, ה-catch רץ, ואז finally.The next line in the try is skipped, the catch runs, then finally.
e.getMessage() נותן את ההודעה בלי שם החריגה.e.getMessage() gives the message without the exception's name.
10 / 0 זורק ArithmeticException. השורה הבאה ב-try מדולגת, ה-catch רץ, ואז finally. e.getMessage() נותן את ההודעה בלי שם החריגה.10 / 0 throws an ArithmeticException. The next line in the try is skipped, the catch runs, then finally. e.getMessage() gives the message without the exception's name.
נושאTopic 5
throw: אתם מחליטים מה לא חוקיthrow: you decide what's illegal
throw: אתם מחליטים מה לא חוקיthrow: you decide what's illegal
מי שקורא למתודה מחליט אם לתפוס.The caller decides whether to catch it.
מתודה שמקבלת ערך לא הגיוני זורקתIllegalArgumentException עם הודעה ברורה: מה מותר ומה התקבל. מי שקורא למתודה מחליט אם לתפוס.A method that gets a nonsense value throws an IllegalArgumentException with a clear message: what's allowed and what came in. The caller decides whether to catch it.
נושאTopic 6
טעויות נפוצותCommon mistakes
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says
`ArithmeticException:`ArithmeticException:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.
נושאTopic 7
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
מדף, צעד 12: שנה לא חוקית לא מפילהShelf, step 12: a bad year doesn't crash
שורות בפורמט Title;Year: Dune;1965, Emma;18x5, Ulysses;1922. הדפיסו כל ספר תקין כמו Dune (1965). על שנה לא חוקית הדפיסו Skipping Emma: bad year 18x5 והמשיכו. בסוף Valid books: 2.Lines as Title;Year: Dune;1965, Emma;18x5, Ulysses;1922. Print each valid book like Dune (1965). For an illegal year print Skipping Emma: bad year 18x5 and go on. Finally Valid books: 2.
try ו-catch: `try { ...try and catch: `try { ...
המונחים של המפגשThis session's terms
finally: בלוק שרץ אחרי try/catch תמיד:finally: A block that runs after try/catch always:
המונחים של המפגשThis session's terms
throw: זורק חריגה בעצמכם:throw: Throws an exception yourself:
המונחים של המפגשThis session's terms
NumberFormatException: נזרקת כש-Integer.parseInt או Double.parseDouble מקבלות טקסט שאינו מספר:NumberFormatException: Thrown when Integer.parseInt or Double.parseDouble get text that isn't a number:
המונחים של המפגשThis session's terms
IllegalArgumentException: החריגה המקובלת כשמתודה מקבלת ערך לא חוקי.IllegalArgumentException: The usual exception when a method gets an illegal value.
אף אחד לא תופס אותה, ולכן התוכנית נעצרת, Java מדפיסה stack trace, והשורה האחרונה לא רצה.Nobody catches it, so the program stops, Java prints a stack trace, and the last line never runs.
חריגה שאף אחד לא תופסAn exception nobody catches
עוטפים את מה שעלול להיכשל ב-try.Wrap what might fail in a try.
try ו-catch: ממשיכים לרוץtry and catch: keep running
כשנזרקת חריגה, Java קופצת ל-catch המתאים, ואחריו התוכנית ממשיכה.When an exception is thrown, Java jumps to the matching catch, and then the program goes on.
try ו-catch: ממשיכים לרוץtry and catch: keep running
בסימולציה רואים את הקפיצה: השורה שאחרי parseInt לא רצה בפעם השנייה.In the simulation you see the jump: the line after parseInt doesn't run the second time.
try ו-catch: ממשיכים לרוץtry and catch: keep running
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.
throw new IllegalArgumentException("bad");throw new IllegalArgumentException("bad");
נכון: throw ועצם חריגה חדש.Right: throw and a new exception object.
המרה עם ברירת מחדלParse with a fallback
כתבו static int parseOr(String s, int fallback) שמחזירה את המספר, או את fallback אם הטקסט אינו מספר.Write static int parseOr(String s, int fallback) returning the number, or fallback if the text isn't a number.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
ממוצע של כלוםAverage of nothing
עם מערך ציונים ריק התוכנית קורסת.With an empty grades array the program crashes.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.
הסדר של finallyThe order of finally
הדפיסו start, ואז ב-try המירו את "x" למספר (והדפיסו parsed אחרי ההמרה).Print start, then in a try convert "x" to a number (printing parsed after the conversion).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.