Error Handling Input-Output Writing Better Code Principles of Programming CM10227 Lecture D.11: Java: Error Handling & Writing Better Code Dr. Marina De Vos University of Bath Ext: 5053 Academic Year 2012-2013 Lecture D.10. (MDV) Programming I Academic Year 2012-2013 1 / 77
Error Handling Input-Output Writing Better Code Resources Objects First with Java. David J. Barnes and Michael K¨ olling. Third edition How to Think Like a Computer Scientist: Java. http://www.greenteapress.com/thinkapjava/ Big Java. Gay Horstman. Thinking in Java. Bruce Eckel’s www.mindview.net/Books/TIJ4 Sun Java Tutorials Series http://java.sun.com/ docs/books/tutorial/index.html Lecture D.10. (MDV) Programming I Academic Year 2012-2013 2 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Outline Error Handling 1 Errors Defensive programming Exception Handling Assertions Input-Output 2 3 Writing Better Code Lecture D.10. (MDV) Programming I Academic Year 2012-2013 3 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Main concepts to be covered Defensive programming. Anticipating that things could go wrong. Exception handling and throwing. Error reporting. Simple file processing. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 4 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Some causes of error situations Incorrect implementation. Does not meet the specification. Inappropriate object request. E.g., invalid index. Inconsistent or inappropriate object state. E.g. arising through class extension. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 5 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Not always programmer error Errors often arise from the environment: Incorrect URL entered. Network interruption. File processing is particular error-prone: Missing files. Lack of appropriate permissions. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 6 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Defensive programming Client-server interaction. Should a server assume that clients are well-behaved? Or should it assume that clients are potentially hostile? Significant differences in implementation required. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 7 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Issues to be addressed How much checking by a server on method calls? How to report errors? How can a client anticipate failure? How should a client deal with failure? Lecture D.10. (MDV) Programming I Academic Year 2012-2013 8 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions An example: The AddressBook Project Create an AddressBook object. Try to remove an entry. A runtime error results. Whose fault is this? Anticipation and prevention are preferable to apportioning blame. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 9 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Argument values Arguments represent a major vulnerability for a server object. Constructor arguments initialize state. Method arguments often contribute to behavior. Argument checking is one defensive measure. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 10 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Checking the key ✞ public void removeDetails ( String key ) { i f ( keyInUse ( key ) ) { ContactDetails d e t a i l s = ( ContactDetails ) book . get ( key ) ; book . remove ( d e t a i l s . getName ( ) ) ; book . remove ( d e t a i l s . getPhone ( ) ) ; numberOfEntries −− ; } } ✝ ✆ ✡ Lecture D.10. (MDV) Programming I Academic Year 2012-2013 11 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Server error reporting How to report illegal arguments? To the user? Is there a human user? Can they solve the problem? To the client object? Return a diagnostic value. Throw an exception. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 12 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Returning a diagnostic ✞ public boolean removeDetails ( String key ) { i f ( keyInUse ( key ) ) { ContactDetails d e t a i l s = ( ContactDetails ) book . get ( key ) ; book . remove ( d e t a i l s . getName ( ) ) ; book . remove ( d e t a i l s . getPhone ( ) ) ; numberOfEntries −− ; return true ; } else { return false ; } } ✝ ✆ ✡ Lecture D.10. (MDV) Programming I Academic Year 2012-2013 13 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Client responses Test the return value. Attempt recovery on error. Avoid program failure. Ignore the return value. Cannot be prevented. Likely to lead to program failure. Exceptions are preferable. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 14 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Exception-throwing principles A special language feature. No special return value needed. Errors cannot be ignored in the client. The normal flow-of-control is interrupted. Specific recovery actions are encouraged. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 15 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Throwing an exception ✞ / ∗∗ ∗ Look up a name or phone number and return the ∗ corresponding contact d e t a i l s . ∗ @param key The name or number to be looked up . ∗ @return The d e t a i l s corresponding to the key , ∗ or n u l l i f there are none matching . ∗ @throws NullPointerException i f the key i s n u l l . ∗ / public ContactDetails getDetails ( String key ) { i f ( key == null ) { throw new NullPointerException ( ” n u l l key in getDetails ” ) ; } return ( ContactDetails ) book . get ( key ) ; } ✝ ✆ ✡ Lecture D.10. (MDV) Programming I Academic Year 2012-2013 16 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Throwing an exception An exception object is constructed: new ExceptionType(”...”); The exception object is thrown: throw ... Javadoc documentation: @throws ExceptionType reason Lecture D.10. (MDV) Programming I Academic Year 2012-2013 17 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions The exception class hierarchy Lecture D.10. (MDV) Programming I Academic Year 2012-2013 18 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Exception categories Checked exceptions Subclass of Exception Use for anticipated failures. Where recovery may be possible. Unchecked exceptions Subclass of RuntimeException Use for unanticipated failures. Where recovery is unlikely. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 19 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions The effect of an exception The throwing method finishes prematurely. No return value is returned. Control does not return to the clients point of call. So the client cannot carry on regardless. A client may catch an exception. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 20 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Unchecked exceptions Use of these is unchecked by the compiler. Cause program termination if not caught. This is the normal practice. IllegalArgumentException is a typical example. Lecture D.10. (MDV) Programming I Academic Year 2012-2013 21 / 77
Errors Error Handling Defensive programming Input-Output Exception Handling Writing Better Code Assertions Argument checking ✞ public ContactDetails getDetails ( String key ) { i f ( key == null ) { throw new NullPointerException ( ” n u l l key in getDetails ” ) ; } i f ( key . trim ( ) . length ( ) == 0) { throw new IllegalArgumentException ( ” Empty key passed to getDetails ” ) ; } return ( ContactDetails ) book . get ( key ) ; } ✝ ✆ ✡ Lecture D.10. (MDV) Programming I Academic Year 2012-2013 22 / 77
Recommend
More recommend