160 likes | 269 Views
CSCI 143. Chapter 15 Exception Handling. Exception. A problem that occurs during program execution An “exception to the rule” An object, with data and methods Is “thrown” by a method or the interpreter Can be “handled” or controlled Creates robust, fault-tolerant, programs
E N D
CSCI 143 Chapter 15 Exception Handling
Exception • A problem that occurs during program execution • An “exception to the rule” • An object, with data and methods • Is “thrown” by a method or the interpreter • Can be “handled” or controlled • Creates robust, fault-tolerant, programs • If uncaught, will crash the program
Exception Handling • try block • enables exception handling • contains statements that might cause exceptions • catch block (exception handler) • specifies an exception type • contains code to deal with “handle” an exception • need one catch for each exception type
Try-Catch Syntax try { //code that might throw an exception } catch (ExceptionType name) { //do something about it } //processing resumes here
Exception Handling • Throw point – where the exception occurs • Try block terminates at throw point • Program looks for a matching catch clause • identical types • exception is a subtype of the declared type • If match is found, catch handler executes • If no match is found, exceptions propagate up the call stack (stack unwinding)
Example String s = keyboard.readString(); try { double d = Double.parseDouble(s); } catch (NumberFormatException e) { System.out.println (“Invalid number”); }
Exception Hierarchy • Throwable • Subclass of object • Superclass for all exceptions • Exception • Can be caught and handled • Example: IOException • Error • Can not typically be caught by the program • Example: OutOfMemoryError
Checked Exceptions • The Java compiler requires Exceptions which are not descended from RuntimeExceptions to be handled
Checked Exceptions public void readData() throws FileNotFoundException { FileReader frd = new FileReader(“test.txt”); } • FileReader constructor might throw an exception • readData() throws the exception to the method that called it • Method that called readData() will either • catch the exception or • throw it further up the call sequence
Unchecked Exceptions • All direct and indirect subclasses of RuntimeException • Package java.lang • Can typically be prevented by proper coding • Are not required to be listed in a method’s throws clause
Practice • Are the following exceptions checked or unchecked? • ArithmeticException • IOException • ClassCastException • InterruptedException • UnsupportedFlavorException • FileNotFoundException
Catching Multiple Exceptions throws FileNotFoundException try { BufferedReader rdr = new BufferedReader (new FileReader (“Text.txt”)); String s = rdr.readLine(); double d = Double.parseDouble (s); } catch (FileNotFoundException e) { System.out.println (“Cannot find file”); } catch (IOException e) { System.out.println (“Cannot read file:” + e.getMessage()); } catch (NumberFormatException e) { System.out.println (“Invalid input”); } throws IOException throws NumberFormatException
Catching Multiple Exceptions • All exceptions can be caught by catching a common superclass try { BufferedReader rdr = new BufferedReader (new FileReader (“Text.txt”)); String s = rdr.readLine(); double d = Double.parseDouble (s); } catch (Exception e) { System.out.println (“Something is wrong!”); } • May result in a loss of error handling precision • May be used to handle errors in the same inheritance hierarchy polymorphically!
finally Clause • Always executes • Used for cleanup (releases resources) • database and network connections, files • Executes before catch clause returns or exits try { //code that might generate an exception } catch { //code to handle the exception } finally { //cleanup here }
In Conclusion • Good programs catch and handle errors • Exception handling is designed to handle runtime errors • Class Exception and its subclasses can be caught and handled • Descendents of RuntimeException are unchecked • Methods can handle their own exceptions or throw them up the call sequence