1 / 32

Exceptions

Exceptions. Ensuring program reliability. Program correctness. The term program correctness refers to a program’s working as advertised; that is, it produces correct results for valid inputs

Download Presentation

Exceptions

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. Exceptions Ensuring program reliability

  2. Program correctness • The term program correctness refers to a program’s working as advertised; that is, it produces correct results for valid inputs • A related concept is program reliability; this is a measure of how well a program performs under a variety of conditions (including invalid inputs) • A program can be correct, but not reliable • A program can be reliable, but not correct • Our goal is to produce programs with both these qualities

  3. Ensuring reliability • We do our best to ensure program correctness through a rigorous testing and debugging process • To ensure reliability, we must anticipate conditions that could cause problems, and try to deal with them before the problems occur • In Java, we have exception handling, a powerful tool for ensuring program reliability

  4. Catching Exceptions • An exception represents an error condition that can occur during the normal course of program execution. • When an exception occurs, or is thrown, the normal sequence of flow is terminated. The exception-handling routine is then executed; we say the thrown exception is caught.

  5. Catching Exceptions • We can increase our programs’ reliability and robustness if we catch the exceptions ourselves using error recovery routines we develop. • One way to do this is to wrap the statements that may throw an exception with the try-catch control statement.

  6. Catching Exceptions - example inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); // code that might cause error } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, “” + inputStr + “ is invalid\n” + “Please enter digits only”); }

  7. Catching Exceptions • A try-catch block is similar to a selection structure • If no error occurs (in the example, if the user entered only digits), the catch block is ignored • If there is an error, the catch block executes

  8. Catching Exceptions • Statements in the try block are executed in sequence. • If more than one exception is possible, multiple catch blocks can be written • When one of the statements throws an exception, control is passed to the matching catch block and statements inside the catch block are executed.

  9. Two control flows of the try-catch statement with one catch block.

  10. Catching Exceptions • We must specify which exception we are catching in the catch block’s parameter list. • In Java an exception is represented as an instance of the Throwable class or its subclasses. • The Throwable class has two subclasses: • Error • Exception

  11. Catching Exceptions • The Error class represents serious problems that should not be caught by ordinary applications • The Exception class represents error conditions that should be caught

  12. Throwable class inheritance hierarchy – a sample

  13. Propagating Exceptions • When a method may throw an exception, either directly or indirectly, we call the method an exception thrower. • Every exception thrower must be one of two types: • catcher. • propagator.

  14. Propagating Exceptions • An exception catcher is an exception thrower that includes a matching catch block for the thrown exception. • An exception propagator does not contain a matching catch block. • A method may be a catcher of one exception and a propagator of another.

  15. Propagating Exceptions • If a method is an exception propagator, we need to modify its header to declare the type of exceptions the method propagates. • We use the reserved word throws for this declaration. void C( ) throws Exception { ... } void D( ) throws Exception { ... }

  16. Propagating Exceptions • Without the required throws Exception clause, the program will not compile. • However, for the exception of the type called runtime exceptions, the throws clause is optional.

  17. Throwing Exceptions • An exception is thrown using the throw statement. throw <a throwable object> • where <a throwable object> is an instance of the Throwable class or its subclasses.

  18. Throwing Exceptions • If there is a block of code that must be executed regardless of whether an exception is thrown, we use the reserved word finally. inputStr = JOptionPane.showInputDialog(null, “”); try{ number = Integer.parseInt(inputStr); if (num>100) { throw new Exception(“Out of bound”); } } catch (NumberFormatException e) { System.out.println(“Cannot convert to int”); } catch (Exception e) { System.out.println(“Error: ” + e.getMessage()); } finally { System.out.println(“DONE”); }

  19. Throwing Exceptions • When there are multiple catch blocks in a try-catch statement, they are checked in sequence. • It is important to check more specialized exception classes before the more general exception classes. • When an exception is thrown, its matching catch block is executed and the other catch blocks are ignored.

  20. Catching Exceptions • There are two methods of the Throwable class we can call to get information about the thrown exception: • getMessage • printStackTrace

  21. 2 possible outcomes of the try-catch statement with multiple catch blocks

  22. Throwing Exceptions • If none of the catch blocks matches the thrown exception, the system will search down the stack trace for a method with a matching catch block. • If none is found, the system will handle the thrown exception.

  23. Throwing Exceptions • Even if there is a return statement inside the try block, the finally block is executed. • When the return statement is encountered in the try block, statements in the finally block are executed before actually returning from the method.

  24. 2 possible outcomes of the try-catch statement with multiple catch blocks and the finally block

  25. Propagating Exceptions • Do not catch an exception that is thrown as a result of violating a condition set by the client programmer. • Instead, propagate the exception back to the client programmer’s code and let him or her handle it.

  26. // Class to input age: allows client programmers to specify the // lower and upper bounds of acceptable input values import javax.swing.*; class AgeInput { private static final String DEFAULT_MESSAGE = "Your age:"; private static final int DEFAULT_LOWER_BOUND = 0; private static final int DEFAULT_UPPER_BOUND = 99; private static final int DEFAULT_LOWER_BOUND = 0; private static final int DEFAULT_UPPER_BOUND = 99; private int lowerBound, upperBound; private void setBounds(int low, int high) { lowerBound = low; upperBound = high; } Example

  27. public AgeInput ( ) throws IllegalArgumentException { setBounds(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND); } public AgeInput (int low, int high) throws IllegalArgumentException { if (low > high) { throw new IllegalArgumentException("Low (" + low + ") was " + "larger than high(" + high + ")"); } else { setBounds(low, high); } } public int getAge() throws Exception { return getAge(DEFAULT_MESSAGE); } Example continued

  28. public int getAge(String prompt)throws Exception { String inputStr; int age; while (true) { inputStr = JOptionPane.showInputDialog(null, prompt); try { age = Integer.parseInt(inputStr); if (age < lowerBound || age > upperBound) { throw new Exception("Input out of bound"); } return age; // input okay so return the value & exit } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "'" + inputStr + " is invalid\n“ + "Please enter digits only"); } // end catch block } // end while loop } // end method } // end class

  29. Types of Exceptions • There are two types of exceptions: • Checked. • Unchecked. • A checked exception is an exception that is checked at compile time. • All other exceptions are unchecked, or runtime, exceptions. As the name suggests, they are detected only at runtime.

  30. Types of Exceptions • If a method is a propagator of checked exceptions, the method must have the throws clause. • When calling a method that can throw checked exceptions, use the try-catch statement and place the call in the try block, or modify the method header to include the appropriate throws clause. • If a method is a propagator of runtime exceptions or errors, the throws clause is optional.

  31. Calling a method that can throw an exception: two options

  32. Calling method that throws a runtime exception – 3 options

More Related