SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Lilian Blot VARIABLE SCOPE EXCEPTIONS FINAL WORD Final Lecture Spring 2014 TPOP 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
CPSC150 Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 CPSC150 Exceptions When things.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Computer Science 340 Software Design & Testing Design By Contract.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Exceptions and error handling. 2 Java exception mechanism when an error or exceptional condition occurs, you throw an exception which is caught by an.
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
© Paul Ammann, 2008 Design by Contract Paul Ammann CS/SWE 332.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Chapter 13 Exception Handling
Handling Exceptionally Sticky Problems
Chapter 13 Exception Handling
Software Development Handing Errors and Creating Documentation
Introduction Exception handling Exception Handles errors
Exception Handling and Event Handling
Syntax, semantics, and pragmatics
Exceptions The Need for Exceptions Throwing Exceptions
Chapter 12 Exception Handling and Text IO
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Design by Contract Fall 2016 Version.
EE422C Software Implementation II
SWE 332 Last Modified Spring 2010 Paul Ammann
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
Exception Handling.
Exception Handling In Text: Chapter 14.
Part B – Structured Exception Handling
Effective Java, 3rd Edition Chapter 10: Exceptions
CSE 143 Java Exceptions 1/18/2019.
Chapter 13 Exception Handling
Go to pollev.com/cse143.
Tenth step for Learning C++ Programming
Chapter 12 Exception Handling and Text IO Part 1
Handling Exceptionally Sticky Problems
Effective Java, Chapter 9: Exceptions
Computer Science 340 Software Design & Testing
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann Exceptions: Bloch SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann

Item 39: Exceptions for Exceptional Conditions Don’t do this! try{ int i=0; while (true) { a[i++].f(); } }catch(IndexOutOfBoundsException e){} Implications for API design Provide state testing method instead of forcing client to catch exception. Kaushik, Ammann 2005

More Item 39: Do this instead for (int i = 0; i < a.length; i++) { a[i].f(); } Note “State Testing method”: i < a.length Kaushik, Ammann 2005

More Item 39: How about: try{ Iterator i = collection.iterator(); while (true) { Foo foo = (Foo) i.next(); } }catch(NoSuchElementException e){} versus: for (Iterator i = collection.iterator(); i.hasNext();){ Foo foo = (Foo) i.next(); ... Kaushik, Ammann 2005

Item 40: Checked vs. Unchecked Unchecked exceptions indicate programming errors Precondition violations Recovery is impossible Checked exceptions indicate recoverable conditions Force the caller to handle the exception Kaushik, Ammann 2005

Checked vs. Unchecked (cont’d) Use unchecked exceptions if client has nothing to do OutOfMemoryException client knows better doesn’t need try-catch block; if-then block would suffice. Use checked exceptions if client has some reasonable action IOException  Calling code is correct, but exception can still be raised! Kaushik, Ammann 2005

Item 41: Avoid Unnecessary Use of Checked Exceptions try{ obj.action(args)} }catch(SomeCheckedException e){ throw new Error (“Assertion Error”) } // should never happen What is the point of making a client do this? Conditions for using checked exceptions: Exceptional condition cannot be prevented by proper use of the API Programmer can take some useful action Kaushik, Ammann 2005

More Item 41: Standard Transformation: if (obj.actionPermitted(args)) { obj.action(args); } else { // Handle exceptional condition } Or even simply (where appropriate): obj.action(args); // Client allows call to fail Kaushik, Ammann 2005

Item 42: Bloch’s standard exceptions IllegalArgumentException - Inappropriate parameter; several special cases NullPointerException (param null where prohibited) IndexOutOfBoundsException (index param out of range) ClassCastException ConcurrentModificationException Concurrent modification detected when not allowed Kaushik, Ammann 2005

More Item 42 IllegalStateException UnsupportedOperationException Object state is inappropriate for method invocation Object may not be initialized before calling accessing its state UnsupportedOperationException Object does not support the method Substitution principal Kaushik, Ammann 2005

More Item 42 Favor the use of standard exceptions Using your API is easier Reading your programs is easier Performance advantage Kaushik, Ammann 2005

Item 43: Exceptions should make sense to the caller! Throw exceptions appropriate to the abstraction Propagated exceptions may make no sense! Higher layers should translate lower level exceptions try{ … }catch(LowerLevelException e){ throw new HigherLevelException(…); } Kaushik, Ammann 2005

Item 44: Document them all! Document all exceptions Liskov and Bloch differ partially Bloch: document each exception thrown (using JavaDoc @throws tag) This is semantically eqvt. to Liskov’s Effects clause Bloch: Don’t mention unchecked exceptions in the signature Liskov: document all exceptions in Effects clause, AND mention in signature Kaushik, Ammann 2005

Item 45: Add helpful info Include failure-capture information in detail messages program failure due to uncaught exception, help messages are printed these call toString() method on exception Create exceptions with relevant data, so that it helps developer using it. In other words, ADD RELEVANT STATE Kaushik, Ammann 2005

Item 46: Failure Atomicity of Procedures A failed method invocation should leave the object in the state that it was in prior to the invocation Ways to achieve this effect: Design immutable objects Check parameters for validity before performing the operation Order the computation – parts may fail come before modification Write recovery code – cause the object to roll back its state Perform the operation on a temporary copy of the object Kaushik, Ammann 2005

Item 46: Atomicity of Procedures public int addMax(int [] a, int x) throws … //Requires: … //Modifies: a //Effects: … Don’t throw exception in between modifications to state variables Procedure should have an atomic effect throw exception in the beginning if you are not sure you can pull it off Kaushik, Ammann 2005

More Item 46 public Object pop() throws … { //Requires: this != EmptyStack //Modifies: this //Effects: pops this and returns top Object result = elements[--size]; elements[size] = null; return result; } // Note: Client can corrupt state – oops! Kaushik, Ammann 2005

More Item 46 public Object pop() throws … { //Requires: //Modifies: this //Effects: If this empty throw ISE // else pop this and returns top if (size == 0) { throw new ISE(…); Object result = elements[--size]; elements[size] = null; return result; } // Note atomic execution, normal or exception Kaushik, Ammann 2005

Item 47: Don’t ignore exceptions Programmers should never leave a catch block empty Its easy to ignore exceptions, but don’t! try{ … }catch(Exception e){ } // empty catch block wont raise complaints from compiler! Don’t do this! Kaushik, Ammann 2005

More Item 47 If its empty, its highly suspect The purpose of exception is to force programmers handle exceptional conditions At least have a comment which explains why its ok to ignore this exception Kaushik, Ammann 2005

Meyer’s View of Exceptions (OO Software Construction) A procedure either succeeds or fails If contract is satisfied, procedure succeeds If not, procedure fails An exception is an event that may cause a procedure to fail. A failure of a procedure causes an exceptional return to the caller. Kaushik, Ammann 2005

More Meyer A procedure will fail if an exception occurs, and the procedure does not recover from the exception. Only two possible responses to an exception Retry Fail (Organized Panic) Only 1 possible exception in Meyer Failure Exception (Compare to Liskov) A bit austere for some tastes - YMMV Kaushik, Ammann 2005