9.22 - CS 367 Announcements Tuesday Homework h1 due 10 pm Friday September 25th Program p1 due 10 pm Sunday September 27th(time is running out Well

9.22 - CS 367 Announcements Tuesday Homework h1 due 10 pm...

This preview shows page 1 out of 10 pages.

You've reached the end of your free preview.

Want to read all 10 pages?

Unformatted text preview: CS 367 Announcements Tuesday, September 22, 2015 Homework h1 due 10 pm Friday, September 25th Program p1 due 10 pm Sunday, September 27th (time is running out!) We’ll discuss program submission Thursday. Assignment questions? Post on Piazza or see a TA during lab consulting hours. Last Time Iterators • coding iterator interfaces and the Java API • using iterators • making a class iterable • options for implementing iterators Today Iterators • options for implementing iterators (from last time) Exceptions Review • throwing • handling • execution • practice Next Time Read: finish Exceptions, start Linked Lists Exceptions Review • throws and checked vs. unchecked • defining Chains of Linked Nodes • Listnode class • practice Compiling & Submitting Programs Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 1 Exception Throwing – Signaling a Problem When a problem is detected we (or RTE run Time Environment) can signal a problem has occurred by throwing an Exception. *When an Exception is thrown Normal Execution ends and switch to exception Handling Execution. Java Syntax throw exceptionObject; Example // consider adding to ArrayBag remove() if(numItems <= 0) // detects the problem throw new EmptyBagException(); // signals the exception Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 2 Exception Handling – Resolving a Problem Try - Catch Statement 1. Try Code that might be problematic 2. Catch exception to handle particular problems Java Syntax try { // try block code that might cause an exception to be thrown } catch (ExceptionType1 identifier1) { // catch block code to handle exception type 1 } catch (ExceptionType2 identifier2) { // catch block code to handle exception type 2 } ... more catch blocks finally { // finally block - optional code always executed when try block is entered } Example // code in ArrayBag Tester main() try{ for(int i = 0; i < 1000; i++){ bag1.remove(); } }catch (EmptyBagException e){ System.out.println("Remove resulted in an EmptyBagException") Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 3 Exception Execution Normal Execution - Start: top of main() - flow through code in the normal manner - Execute: Normal Code, code in try blocks, code in any finally blocks WHEN TRY WAS ENTERED - Skip: - code in catch blocks - Switch to Exception Handling Execution When an exception is thrown Exception Handling Execution - Skip: Normal Code including any remaining code in try blocks that have been entered - Execute: Do code in finally locks those try was entered - Switch back to Normal Execution When the exception is caught Searching for a Matching Catch 1. Locally - if the code causing the exception is in a try block then check its catches for a match - else terminate the method and throw the exception to its caller 2. Remotely if a method call returns with an exception and the call is in a try block then check its catches for a match Else we terminate the method and throw the exception to its caller 3. Match Checking - done from the top catch to bottom a. Match Found - Switch Back to normal execution -execute code in catch block, and corresponding finally block (if any) - continue execution after that try-catch statement b. No Match Found - execute code in corresponding finally block (if any) - Terminate the method and throw its exception to its caller. Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 4 ExceptionTester Example public class ExceptionTester { public static void main(String args) { System.out.print("main["); try { methodA( ); System.out.print("after A,"); methodE( ); System.out.print("after E,"); } catch (RedException exc) { System.out.print("main-red,"); } catch (GreenException exc) { System.out.print("main-green,"); } finally { System.out.print("main-finally,"); } System.out.println("]main"); } private static void methodA( ) { System.out.print("\nA["); try { methodB( ); System.out.print("after B,"); } catch (BlueException exc) { System.out.print("A-blue,"); } System.out.println("]A"); } private static void methodB( ) { System.out.print("\nB["); methodC( ); System.out.print("after C,"); try { methodD( ); System.out.print("after D,"); } catch (YellowException exc) { System.out.print("B-yellow,"); throw new GreenException(); } catch (RedException exc) { System.out.print("B-red,"); } finally { System.out.print("B-finally,"); } System.out.println("]B"); } Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 5 What is Output When: 1. no exception is thrown main[ A[ B[ after C, after D, B- finally] B after B,]A after A, after E, ] main 2. methodE throws a YellowException? main[ A[ B[ after C, after D, B - finally, ]B, after B, ]A, after A, main - finally, CRASH! 3. methodC throws a GreenException? main[ A[ B[ 4. methodD throws a GreenException? main[ A[ B[ Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 6 What is Output When: 5. methodC throws a RedException? main[ A[ B[ 6. methodD throws a RedException? main[ A[ B[ 7. methodD throws a YellowException? main[ A[ B[ after C, B - yellow, B - finally, main - green, main - finally, ] main 8. methodD throws a OrangeException? main[ A[ B[ Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 7 What is Output When: 9. methodC throws a YellowException? main[ A[ B[ 10. methodC throws a BlueException? main[ A[ B[ 11. methodE throws a RedException? main[ A[ B[ Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 8 throws clause – Passing the Buck Checked vs. Unchecked Unchecked: for problems that can and should be avoided with careful programming. eg. NullPointerException, IndexOutOfBoundsException Checked: For problems that cannot be avoided with careful programming eg. IOException, FileNotFoundException compiler checks that the programmer is aware by looking for 1 0f 2 things: 1. Catch block 2. throws clause Java Syntax ... methodName(parameter list) throws ExceptionType1, ExceptionType2, ... { ... } Example public static void main(String args) throws IOException { ... Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 9 Defining a New Exception Class Checked public class MyException extends Exception { } Unchecked public class MyException extends RuntimeException { } Example public class EmptyBagException extends Exception { public EmptyBagException() { super(); } public EmptyBagException(String msg) { super(msg); } } if(...){ throw new EmptyBagException("Empty Bag"); Copyright 2014-2015 Jim Skrentny CS 367 (F15): L6 - 10 ...
View Full Document

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture