View
218
Download
0
Embed Size (px)
Citation preview
CPSC150
Week 13
Chapter 12
Exceptions
(from slides provided by textbook web site)
CPSC150
Handling errors
2.1
CPSC150
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.
CPSC150
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.
CPSC150
Exploring errors
• Explore error situations through the address-book projects.
• Two aspects:– Error reporting.– Error handling.
CPSC150
Issues to be addressed
• How much checking by a server on method calls?
• How to report errors?
What if no user at the other end (e.g., GUI)?
• How can a client anticipate failure?
• How should a client deal with failure?
CPSC150
An example
• 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.
CPSC150
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.
CPSC150
Checking the key
public void removeDetails(String key){ if(keyInUse(key)) { ContactDetails details = (ContactDetails) book.get(key); book.remove(details.getName()); book.remove(details.getPhone()); numberOfEntries--; }}
CPSC150
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
CPSC150
Returning a diagnostic(Exception handling without using
Java’s exception handling)public boolean removeDetails(String key){ if(keyInUse(key)) { ContactDetails details = (ContactDetails) book.get(key); book.remove(details.getName()); book.remove(details.getPhone()); numberOfEntries--; return true; } else { return false; }}
CPSC150
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.
• Create something client cannot ignore: – an exception
CPSC150
Exception-throwing principles
• Exceptions are part of many languages, central to Java.
• 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.
CPSC150
Exceptions
• Exceptions are “thrown” by the method finding the problem
• Exceptions are “caught” by the method dealing with the problem
• maintains integrity and decoupling
CPSC150
The effect of an exception
• The throwing method finishes prematurely.
• No return value is returned.
• Control does not return to the client’s point of call.– So the client cannot carry on regardless.
• A client may ‘catch’ an exception.
CPSC150
Throwing an exception
/** * Look up a name or phone number and return the * corresponding contact details. * @param key The name or number to be looked up. * @return The details corresponding to the key, * or null if there are none matching. * @throws NullPointerException if the key is null. */public ContactDetails getDetails(String key){ if(key == null){ throw new NullPointerException( "null key in getDetails"); } return (ContactDetails) book.get(key); }
CPSC150
Throwing an exception
• An exception object is constructed:– new ExceptionType("...");
• The exception object is thrown:– throw ...
• Javadoc documentation:– @throws ExceptionType reason
CPSC150
Error recovery
• Clients should take note of error notifications.– Check return values.– Don’t ‘ignore’ exceptions.
• Include code to attempt recovery.– Will often require a loop.
CPSC150
The try statement
• Clients catching an exception must protect the call with a try statement:
try { Protect one or more statements here.}catch(Exception e) { Report and recover from the exception here.}
CPSC150
The try statement
try{ addressbook.saveToFile(filename); tryAgain = false;}catch(IOException e) { System.out.println("Unable to save to " + filename); tryAgain = true;}
1. Exception thrown from here
2. Control transfers to here
CPSC150
try { // code that might throw an exception {catch (TypeOfException ex) { // what to do when there is a problem }// what to do afterwards regardless of // whether there was an exception
CPSC150
try { // code that might throw an exception }catch (TypeOfException ex) { // what to do when there is a problem }// what to do afterwards regardless of // whether there was an exception
Order of statement execution
1. if method call, do method
2a. continue with rest of try
3a. skip catch, do code after try-catch
Case a: no exception thrown
CPSC150
try { // code that might throw an exception {catch (TypeOfException ex) { // what to do when there is a problem }// what to do afterwards regardless of // whether there was an exception
Order of statement execution
1. if method call, do method
2b. skip rest of try; do code in catch
3b. if catch code does not abort program, continue after catch block
Case b: TypeOfException thrown
CPSC150
try { // code that might throw an exception {catch (TypeOfException ex) { // what to do when there is a problem }// what to do afterwards regardless of // whether there was an exception
Order of statement execution
1. if method call, do method
2c. skip rest of try; do not do catch; return to method that called this method; exit from this method at the code in try; do not do any other code
3c. No other code in this method is executed. catch code and code afterwards is skipped
Case c: DifferentException thrown
CPSC150
public class Myexcept{private int x;
public Myexcept() { x = 0; }
public int divideByY(int y) {
if (y == 0) { throw new ArithmeticException( "in divideByY, y, a denominator, is 0"); }
y= x /y; return y; }
An Exception Example
CPSC150
At the Board
• Write a main method that calls divideByY
• Write a main method that has try-catch block
public class Myexceptpublic class Myexcept{{private int x;private int x;
public Myexcept()public Myexcept() { x = 0; }{ x = 0; }
public int divideByY(int y)public int divideByY(int y) {{
if (y == 0) {if (y == 0) { throw new ArithmeticException(throw new ArithmeticException( "in divideByY, y, a denominator, is 0"); }"in divideByY, y, a denominator, is 0"); } y= x /y;y= x /y; return y;return y; }}
CPSC150
public static void main(String[] args)
{
Myexcept m = new Myexcept();
System.out.println("m with 4 is " + m.divideByY(4));
System.out.println("m with 0 is " + m.divideByY(0));
System.out.println("all done");
} // end main
} // end class
CPSC150
public static void main(String args[ ]) {
try {System.out.println("m with 4 is " +
m.divideByY(4)); System.out.println("m with 0 is " +
m.divideByY(0)); } catch (ArithmeticException e) { System.out.println("don't call sample method with
0");e.printStackTrace();
} System.out.println("all done"); }
CPSC150
Attempting recovery// Try to save the address book.boolean successful = false;int attempts = 0;do { try { addressbook.saveToFile(filename); successful = true; } catch(IOException e) { System.out.println("Unable to save to " + filename); attempts++; if(attempts < MAX_ATTEMPTS) { filename = an alternative file name; } }} while(!successful && attempts < MAX_ATTEMPTS);if(!successful) { Report the problem and give up;}
CPSC150
public class Test { public static void main(String args[]) { int n = 0; String s = null; boolean valid = false; while (!valid && n<3) try { s = JOptionPane.showInputDialog( null, "Enter an integer:" ); n = Integer.parseInt( s ); valid = true; } catch (Exception e) {
JOptionPane.showMessageDialog( null, "Enter a valid number" ); n++;
} // end of loop if (valid) JOptionPane.showMessageDialog( null, "The number is " + n ); }}
CPSC150
Catching multiple exceptionstry in order that they appear
try { ... ref.process(); ...}catch(EOFException e) { // Take action on an end-of-file exception. ...}catch(FileNotFoundException e) { // Take action on a file-not-found exception. ...}
CPSC150
The finally clause
try { Protect one or more statements here.}catch(Exception e) { Report and recover from the exception here.}finally { Perform any actions here common to whether or not an exception is thrown.}
CPSC150
The finally clause: why
• A finally clause is executed even if a return statement is executed in the try or catch clauses.
• A uncaught or propagated exception still exits via the finally clause.
CPSC150
The exception class hierarchy
CPSC150
Exception categories
• Checked exceptions– Subclass of Exception– Use for anticipated failures.– Where recovery may be possible.– Often not a programming problem (e.g., file not found)
• Unchecked exceptions– Subclass of RuntimeException– Use for unanticipated failures.– Where recovery is unlikely.– Often, a programming problem (e.g., index out of bounds)
• Third category: errors
CPSC150
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.
CPSC150
Argument checking
public ContactDetails getDetails(String key){ if(key == null) { throw new NullPointerException( "null key in getDetails"); } if(key.trim().length() == 0) { throw new IllegalArgumentException( "Empty key passed to getDetails"); } return (ContactDetails) book.get(key);}
CPSC150
Checked Exceptions
• Previous examples all unchecked exceptions– unchecked exceptions should not happen; mostly
result in program termination
• Checked exceptions are meant to be caught.• The compiler ensures that their use is tightly
controlled.– In both server and client.
• Used properly, failures may be recoverable.
CPSC150
Some Checked Exceptions
– ClassNotFoundException– IOException– NoSuchFieldException– ServerNotActiveException– UnsupportedFlavorException
CPSC150
The throws clause
• Methods throwing a checked exception must include a throws clause:
public void saveToFile(String destinationFile) throws IOException
• (NOT throw) throw is when the exception is thrown
CPSC150
throws
• must include throws in methods that throw checked exceptions
• how do I know?
• javadoc/syntax errors
CPSC150
// file output. method called from constructorpublic void writeto(String fileout, int nbrlines) throws
IOException// must have “throws IOException” even though // no “throw statement” { File outfile = new File(fileout); FileWriter fw = new FileWriter(outfile); // usually a loop to write out all information // write to writer, not file fw.write("There were " + nbrlines + " lines in the input
file."); fw.close(); }
CPSC150
Files
Chapter 12 continued
CPSC150
Text input-output:tie to exceptions
• Input-output is particularly error-prone.– It involves interaction with the external
environment.
• The java.io package supports input-output.
• java.io.IOException is a checked exception.
• cannot do files without exceptions
CPSC150
Files
• To use files, use File class
• File class can’t read/write
• To read/write files, use Reader/Writer classes
• File input can be text-based (what we’ll do), stream-based
CPSC150
Readers, writers, streams
• Readers and writers deal with textual input.– Based around the char type.
• Streams deal with binary data.– Based around the byte type.
• The address-book-io project illustrates textual IO.
CPSC150
Text output
• Use the FileWriter class.– Tie File object to diskfile name– Open a FileWriter; tie to File.– Write to the FileWriter.– Close the FileWriter.
• Failure at any point results in an IOException.
CPSC150
Text output
try { FileWriter writer = new FileWriter("name of file"); while(there is more text to write) { ... writer.write(next piece of text); ... } writer.close();}catch(IOException e) { something went wrong with accessing the file}
CPSC150
Text input
• Use the FileReader class.• Augment with BufferedReader for line-
based input.– Open a file.– Read from the file.– Close the file.
• Failure at any point results in an IOException.
CPSC150
Text input
try { BufferedReader reader = new BufferedReader( new FileReader( new File("filename"))); String line = reader.readLine(); while(line != null) { do something with line line = reader.readLine(); } reader.close();}catch(FileNotFoundException e) { the specified file could not be found}catch(IOException e) { something went wrong with reading or closing}
CPSC150
import java.io.*;
public class countlines{
// all methods that do something inserted here
public static void main(String[] args) { if (args.length != 2) { System.out.println("to run, type: 'countlines filein fileout'"); System.exit(1); }// create a new countline object with first arg as input file, second as
output countlines cl = new countlines(args[0], args[1]);
}}
CPSC150
Write a program to count number of lines in a file and write the number
to an output filejava countlines myinfile.txt myoutfile.txt
would return 2 if file is:
This is a file
with two lines.
myinfile.txt
CPSC150
// constructorpublic countlines(String filein, String fileout) { try { int countwords = readfrom(filein); writeto(fileout, countwords); } catch (IOException e) { // will catch IO exception not caught by readfrom and writeto
System.out.println("Problems with IO. " + " Program aborted without successful
write."); } }
CPSC150
public int readfrom(String filename) throws IOException { int countlines = 0; try { File infile = new File(filename); BufferedReader reader = new BufferedReader(new
FileReader(infile)); String line; line = reader.readLine(); while (line != null) { System.out.println(line); // for debugging only; countlines++; line = reader.readLine(); } reader.close(); } catch (FileNotFoundException e) { System.out.println("The input file was not there"); } return countlines; }
CPSC150
Other Wrappers
• Can break string read up using StringTokenizer
• Can read non-text files with FileReader and Tokenizer (breaks fields up to be read)
CPSC150
Console input Scanner class: Java 5
import java.util.*; class MyScanner { public MyScanner ( ) {
Scanner scanner = new Scanner(System.in); System.out.println("Enter a number: "); int x = scanner.nextInt(); int y = scanner.nextInt();
System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); }}
CPSC150
File input using Scannerimport java.util.*; import java.io.*;class MyScanner { public MyScanner ( ) {
Scanner scanner; try { scanner = new Scanner(new File("infile.java"));
int x = scanner.nextInt( ); int y = scanner.nextInt( );
System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); }
catch (FileNotFoundException ex) { System.out.println("File not found ...aborting program."); System.exit(1); } }}