Chapter 11 Exception Handling I. Scott MacKenzie 1 Outline Last slide on Mar 22 2 1
Runtime Errors // From Figure 11.1 in course text public class ExceptionHandling01 IO.println("Enter a string containing a slash"); IO.println("Left substring: " + left); IO.println("Right substring: " + right); ExceptionHandling01.java Demo 3 Extracting Substrings Exception 4 2
substring API The API of this method states that if certain conditions are not met, an exception of a specified type is thrown. This means the method does not handle the exception; it delegates that to the caller 5 try catch construct 6 3
Using the try catch Construct // From Figure 11.5 in course text public class ExceptionHandling02 IO.println("Enter a string containing a slash"); try IO.println("Left substring: " + left); IO.println("Right substring: " + right); catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); //IO.println(e); IO.println("Exiting application. Have a nice day!"); ExceptionHandling02.java Demo 7 Multiple catch Construct 8 4
// From Figure 11.7 in course text Reading a Fraction public class ExceptionHandling03 IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); IO.println("Exiting application. Bye."); Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 ExceptionHandling03.java Demo 9 Reading a Fraction (Robust Version) // From Figure 11.8 in course text public class ExceptionHandling04 try IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); catch (NumberFormatException e) IO.println("Non-integer operands"); catch (ArithmeticException e) IO.println("Cannot divide by zero!"); IO.println("Exiting application. Bye."); ExceptionHandling04.java Demo Last slide on Mar 24 Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 10 5
Reading a Fraction (Friendly Version) // From Figure 11.8 in course text public class ExceptionHandling05 boolean finished = false; while (!finished) try IO.println("Enter a fraction"); int leftint = Integer.parseInt(left); int rightint = Integer.parseInt(right); int answer = leftint / rightint; IO.println("Quotient = " + answer); finished = true; catch (IndexOutOfBoundsException e) IO.println("No slash in input!"); catch (NumberFormatException e) IO.println("Non-integer operands"); catch (ArithmeticException e) ExceptionHandling05.java Demo Note: Different inputs yield different exceptions. Try 123.456 123/abc 123/0 11 Throwable Hieararchy 12 6
Without Exception Handling 13 Wrapped Exceptions The application throws a highly specialized exception called SQLException. To hide its details, the application creates an ordinary runtime exception and wraps the specialized exception in it as the cause. 14 7
Logic Errors Review: Three types of errors: Syntax (produce compile errors) Runtime (produce exceptions) Logic Logic errors are most troublesome because they produce no visible sign other than incorrect behaviour or results Logic errors can be converted to runtime errors by designing programs according to S/E principles; i.e., by asserting assumptions. In so doing, logic errors throw exceptions which can be caught and handled accordingly 15 Last slide on Mar 26 Thank You 16 8