1 / 16

CSCI 143

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

wilma
Download Presentation

CSCI 143

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. CSCI 143 Chapter 15 Exception Handling

  2. 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

  3. 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

  4. Try-Catch Syntax try { //code that might throw an exception } catch (ExceptionType name) { //do something about it } //processing resumes here

  5. 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)

  6. Example String s = keyboard.readString(); try { double d = Double.parseDouble(s); } catch (NumberFormatException e) { System.out.println (“Invalid number”); }

  7. 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

  8. Exception Hierarchy

  9. Checked Exceptions • The Java compiler requires Exceptions which are not descended from RuntimeExceptions to be handled

  10. 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

  11. 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

  12. Practice • Are the following exceptions checked or unchecked? • ArithmeticException • IOException • ClassCastException • InterruptedException • UnsupportedFlavorException • FileNotFoundException

  13. 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

  14. 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!

  15. 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 }

  16. 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

More Related