The exception hierarchy, throws, your own exceptions, and try-with-resources that closes resources by itself.
כותבים בעורך שבכל תרגיל, לוחצים הרצה כדי לראות פלט, ובדיקה כדי שהבודק האוטומטי יבדוק. אפשר גם לעבוד ב-IntelliJ ולסמן פתרתי. ההתקדמות נשמרת.Write in each exercise's editor, press Run to see output and Check for the automatic checker. You can also work in IntelliJ and mark I solved it. Progress is saved.
חימום: 3 שאלותWarm-up: 3 questions
תרגיליםExercises
תרגילExercise 1
שנה לא חוקיתAn invalid year
כתבו את InvalidYearException (checked, עם בנאי להודעה ובנאי להודעה וסיבה) ואת parseYear מהתזכורת. עבור 1965, 3000, 19x5 הדפיסו:
ok 1965 / bad: year out of range: 3000 / bad: not a year: 19x5 (cause: NumberFormatException).
Write InvalidYearException (checked, with a message constructor and a message-and-cause constructor) and parseYear from the refresher. For 1965, 3000, 19x5 print:
ok 1965 / bad: year out of range: 3000 / bad: not a year: 19x5 (cause: NumberFormatException).
איך הפלט אמור להיראות?What should the output look like?
ok 1965
bad: year out of range: 3000
bad: not a year: 19x5 (cause: NumberFormatException)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
התוכנית מהתזכורת לא מתקמפלת. תפסו את ה-IOException ב-main, טענו גם "", והדפיסו data of shelf.txt ואז could not load: no file name.
The program from the refresher doesn't compile. Catch the IOException in main, load "" too, and print data of shelf.txt then could not load: no file name.
איך הפלט אמור להיראות?What should the output look like?
data of shelf.txt
could not load: no file name
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
משאב Res implements AutoCloseable מדפיס open ו-close. פתחו db ו-file ב-try-with-resources אחד, הדפיסו working with db and file, ואחרי הבלוק done. הפלט: open db, open file, working with db and file, close file, close db, done.
A resource Res implements AutoCloseable prints open and close. Open db and file in one try-with-resources, print working with db and file, and after the block done. The output: open db, open file, working with db and file, close file, close db, done.
איך הפלט אמור להיראות?What should the output look like?
open db
open file
working with db and file
close file
close db
done
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:try (Res a = new Res("db"); Res b = new Res("file")) { ... }try (Res a = new Res("db"); Res b = new Res("file")) { ... }
מתי checked ומתי uncheckedWhen checked, when unchecked
setRating זורקת IllegalArgumentException (באג אצל מי שקרא), ו-find זורקת NotFoundException checked (מצב צפוי). הדפיסו:
rating 4 / bug in the caller: rating must be 1-5, got 9 / Dune by Herbert / expected situation: no book called Hamlet.
setRating throws IllegalArgumentException (a bug in the caller), and find throws a checked NotFoundException (an expected situation). Print:
rating 4 / bug in the caller: rating must be 1-5, got 9 / Dune by Herbert / expected situation: no book called Hamlet.
איך הפלט אמור להיראות?What should the output look like?
rating 4
bug in the caller: rating must be 1-5, got 9
Dune by Herbert
expected situation: no book called Hamlet
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:רק ל-find צריך throws בכותרת.Only find needs throws in its header.
sumYears סוכמת שנים משורות Title;Year. על שורה פגומה היא זורקת DataException עם מספר השורה והסיבה המקורית (multi-catch של NumberFormatException | ArrayIndexOutOfBoundsException). הדפיסו 3780 ואז line 2: bad record 'Emma' <- ArrayIndexOutOfBoundsException.
sumYears sums years from Title;Year lines. On a broken line it throws a DataException with the line number and the original cause (a multi-catch of NumberFormatException | ArrayIndexOutOfBoundsException). Print 3780 then line 2: bad record 'Emma' <- ArrayIndexOutOfBoundsException.
איך הפלט אמור להיראות?What should the output look like?
3780
line 2: bad record 'Emma' <- ArrayIndexOutOfBoundsException
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:throw new DataException("line " + (i + 1) + ..., e);throw new DataException("line " + (i + 1) + ..., e);
מדף, צעד 28: BookNotFoundExceptionShelf, step 28: BookNotFoundException
פרויקט מלווהCompanion project
BookNotFoundException (checked) שומרת את הכותר המבוקש ובונה הודעה. Shelf.find מחזירה ספר או זורקת. הדפיסו Found: Book[title=Emma, author=Austen] ואז No book titled 'Hamlet' on the shelf (asked for: Hamlet).
BookNotFoundException (checked) keeps the requested title and builds a message. Shelf.find returns a book or throws. Print Found: Book[title=Emma, author=Austen] then No book titled 'Hamlet' on the shelf (asked for: Hamlet).
איך הפלט אמור להיראות?What should the output look like?
Found: Book[title=Emma, author=Austen]
No book titled 'Hamlet' on the shelf (asked for: Hamlet)
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:שדה title בחריגה, עם getter.A title field in the exception, with a getter.
הגוף של try-with-resources זורק body failed, וגם close() זורק close failed. איזו חריגה מגיעה ל-catch? הדפיסו body failed ואז suppressed: close failed (עם getSuppressed()).
The body of a try-with-resources throws body failed, and close() throws close failed too. Which exception reaches the catch? Print body failed then suppressed: close failed (with getSuppressed()).
איך הפלט אמור להיראות?What should the output look like?
body failed
suppressed: close failed
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:החריגה מהגוף "מנצחת", ושל close מצורפת אליה.The body's exception "wins", and close's is attached to it.
risky(s) מחזירה 100 / parseInt(s). עבור 4, 0, x תפסו את שתי החריגות האפשריות ב-catch אחד, ו-finally מדפיס checked .... הפלט: 25, checked 4, 0: ArithmeticException, checked 0, x: NumberFormatException, checked x.
risky(s) returns 100 / parseInt(s). For 4, 0, x catch both possible exceptions in one catch, and finally prints checked .... The output: 25, checked 4, 0: ArithmeticException, checked 0, x: NumberFormatException, checked x.
איך הפלט אמור להיראות?What should the output look like?
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.