Clickers on Exceptions with Highlights
This presentation delves into handling exceptions in Java code, featuring scenarios involving enrolling in courses and exception handling. Detailed explanations and code executions are provided to enhance understanding.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Clickers on Exceptions (with answers) The first slide is the code that we posted on the document projector The correct answers are highlighted on each question After the first question, there s a marked-up version of the code showing the order in which lines get executed
LinkedList<String> courses = new LinkedList<String>(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // enroll in one course boolean register (String newCourse) throws AlreadyInCourseExn{ if (courses.contains(newCourse)) { thrownew AlreadyInCourseExn(newCourse); } else { courses.add(newCourse); } System.out.println( Registered for + newCourse); return true; } // enroll in multiple courses void pickCourses() { try { register( CS2102 ); register( CS2102 ); register( MU1300 ); } catch (AlreadyInCourseExn e) { System.out.println( Can t take same course twice! ); } System.out.println( Registration complete ); System.out.println( You are taking + courses); }
Clicker: After calling pickCourses, what is in courses (i.e., what courses print out in line 24)? A. CS2102 (one time) B. CS2102 (two times) C. CS2102 (1 or 2 times) and MU1300 D. only MU1300 E. courses is empty see the next slide for the explanation of this answer the slide numbers the main lines of code that execute, in order (see the bold red annotations)
LinkedList<String> courses = new LinkedList<String>(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 // enroll in one course boolean register (String newCourse) throws AlreadyInCourseExn{ if (courses.contains(newCourse)) { thrownew AlreadyInCourseExn(newCourse); [3] thrown } else { courses.add(newCourse); } System.out.println( Registered for + newCourse); return true; } third call to register never happens after the catch, Java runs the code after the try/catch block it doesn t go back into the try block. // enroll in multiple courses void pickCourses() { try { register( CS2102 ); [1] this call finishes fine register( CS2102 ); [2] this call starts register( MU1300 ); } catch (AlreadyInCourseExn e) { [4] Java jumps here on throw System.out.println( Can t take same course twice! ); [5] } System.out.println( Registration complete ); [6] after catch System.out.println( You are taking + courses); } [7] only the first CS2102 is in courses
Clicker: If the exception is thrown in line 6 of register, what is the next line that Java executes? A. 10 B. 18 C. 21 [see previous slide for explanation] D. 23 E. we can t know without knowing which call to register yielded the exception
Clicker: If pickCourses were changed to call register with three distinct courses, how often would line 21 be executed? A. never [catch code isn t run unless a throw occurs] B. when every course is added C. once after the last course is added D. something else
Clicker: If pickCourses were changed to call register with the same course three times, how often would line 21 be executed? A. never B. after each call to register C. only after the first call to register D. only after the second call to register E. only after the last call to register