1 / 22

David Evans cs.virginia/evans

Lecture 10: Programming Exceptionally. David Evans http://www.cs.virginia.edu/evans. CS201j: Engineering Software University of Virginia Computer Science. Last Time…. No checking Assume programmers know what they are doing Run-time checking

sylvialewis
Download Presentation

David Evans cs.virginia/evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: Programming Exceptionally David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science

  2. Last Time… • No checking • Assume programmers know what they are doing • Run-time checking • Check for anomalous behavior during program execution • Static checking • Check at compile-time • Know properties of all possible executions before executing code CS 201J Fall 2003

  3. Exceptions in Java CS 201J Fall 2003

  4. StringSet choose public class StringSet { Vector els; // a Vector of String objects //@invariant els != null //@invariant els.elementType == \type(String) //@invariant els.containsNull == false … public String choose () // EFFECTS: Returns an element of this. { return (String) els.firstElement (); } } CS 201J Fall 2003

  5. What can go wrong… public static void test () { StringSet s = new StringSet (); s.insert ("Alpha"); s.remove (s.choose ()); s.remove (s.choose ()); } > java TestClient Exception in thread "main" java.util.NoSuchElementException at java.util.Vector.firstElement(Vector.java:450) at StringSet.choose(StringSet.java:54) at TestClient.test(TestClient.java:22) at TestClient.main(TestClient.java:4) CS 201J Fall 2003

  6. public class StringSet { Vector els; // a Vector of String objects //@invariant els != null //@invariant els.elementType == \type(String) //@invariant els.containsNull == false … public String choose () // REQUIRES: this has at least one element // EFFECTS: Returns an element of this. { return (String) els.firstElement (); } } CS 201J Fall 2003

  7. Use Exceptions to Remove Requires public String choose () throws EmptyException // EFFECTS: If this has at least one // element, returns an element of this. // Otherwise, throws EmptyException. CS 201J Fall 2003

  8. Throwing Exceptions public String choose () throws EmptyException // EFFECTS: If this has at least one element, returns an // element of this. Otherwise, throws EmptyException. { if (size () == 0) throw new EmptyException (); return (String) els.firstElement (); } What is EmptyException? CS 201J Fall 2003

  9. Exceptions are Objects public class EmptyException extends Exception { public EmptyException () { super (); } } Exception EmptyException extends Exception means EmptyException inherits from the Exception type (in the Java API). We will cover subtyping and inheritance next week. CS 201J Fall 2003

  10. Catching Exceptions public class SetClient { public static void test () { StringSet s = new StringSet (); s.insert ("Alpha"); try { s.remove (s.choose ()); s.remove (s.choose ()); } catch (EmptyException e) { System.err.println ("Got EmptyException!"); System.exit (1); } System.out.println (“Done”); } } Code inside the try block executes normally until it throws an exception. If no exception is thrown, execution proceeds after the catch. If the EmptyException exception is thrown, the catch handler runs. CS 201J Fall 2003

  11. Propagating Exceptions public class StringSet { public String choose () { return (String) els.firstElement (); } } throws Vector.firstElement NoSuchElementException looking for catch handler public class SetClient { public static void main (String []args) { StringSet s = new StringSet (); s.insert ("Alpha"); s.remove (s.choose ()); s.remove (s.choose ()); System.out.println (“Done”); } } StringSet.choose looking for catch handler calls SetClient.main looking for catch handler Exception in thread "main" java.util.NoSuchElementException at java.util.Vector.firstElement(Vector.java:450) at StringSet.choose(StringSet.java:54) at SetClient.main(SetClient.java:6) CS 201J Fall 2003

  12. Checked Exceptions • Java has two types of exceptions: checked exceptions and run time exceptions • Checked exceptions must be caught • Java compiler will not allow a program that could have an unchecked checked exception (so they don’t propagate to caller) • Run time exceptions need not be caught • Subtype of RuntimeException • Propagate automatically up stack until caught CS 201J Fall 2003

  13. Catching Exceptions public class SetClient { public static void main (String args[]) { StringSet s = new StringSet (); s.insert ("Alpha"); System.out.println (s.choose ()); } } > javac SetClient.java SetClient.java:5: unreported exception EmptyException; must be caught or declared to be thrown CS 201J Fall 2003

  14. Guidelines • Use unchecked exceptions when the exception is not part of the client interface: • Specified precondition is violated • Defensive programming • Assertion violated • Use checked exceptions when: • An unusual situation prevents the implementation from satisfying the normal postcondition CS 201J Fall 2003

  15. Does Java API follow our guidelines? public final Object firstElement() // EFFECTS: If this vector has no elements, throws // NoSuchElementException. Otherwise, returns // the first component of this vector. public class StringSet { Vector els; // a Vector of String objects public String choose () // EFFECTS: Returns an element of this. { return (String) els.firstElement (); } } NoSuchElementException is a Runtime Exception, so there is no compiler warning for choose. CS 201J Fall 2003

  16. Pop Quiz! CS 201J Fall 2003

  17. Specifying Exceptional Behavior • Checked exceptions are part of the client interface: should be specified • ESC/Java exsures annotation: //@ensures N //@exsures (ExceptionType) E If the procedure returns normally, the postcondition N is true. If the procedure throws an exception of type ExceptionType, E is true. CS 201J Fall 2003

  18. Specifying Choose public String choose () throws EmptyException //@ensures \result != null //@exsures (EmptyException) numEntries == 0 { if (size () == 0) throw new EmptyException (); return (String) els.firstElement (); } CS 201J Fall 2003

  19. Exceptions Considered Harmful • Interfaces are more complicated – caller needs to worry about possible exceptions as well as result • Makes it harder to understand programs • Control flow jumps around like a goto CS 201J Fall 2003

  20. PS2 AverageLength public class AverageLength { public static void main (/*@non_null@*/ String args[]) throws RuntimeException { String filename = args[0]; try { FileInputStream infile = new FileInputStream (filename); StringTable names = new StringTable (infile); int numnames = names.size (); int totallength = 0; for (int index = 0; index <= numnames; index++) { String name = names.getNthLowest (index); totallength = totallength + name.length (); } System.out.println ("The average name length is: " + (double) totallength / numnames); } catch (FileNotFoundException e) { System.err.println ("Cannot find file: " + filename); System.exit (1); } } } CS 201J Fall 2003

  21. Exceptions Considered Helpful • Provide a way to deal with abnormal conditions • Better than returning “special” values since caller may forget to check for them • Allow you to deal with errors (e.g., file not found) up the call stack where more context information is available • Separate normal code from error handling CS 201J Fall 2003

  22. Charge • PS4 Design Documents due today • In Section Friday, you will discuss your design with another team • Similarities and differences • What is better/worse about each design • What will make it more/less difficult to implement correctly CS 201J Fall 2003

More Related