Top Banner
Exceptions Ensuring program reliability
32

Exceptions

Jan 01, 2016

Download

Documents

dane-chandler

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exceptions

Exceptions

Ensuring program reliability

Page 2: Exceptions

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

Page 3: Exceptions

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

Page 4: Exceptions

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.

Page 5: Exceptions

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.

Page 6: Exceptions

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”);

}

Page 7: Exceptions

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

Page 8: Exceptions

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.

Page 9: Exceptions

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

Page 10: Exceptions

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

Page 11: Exceptions

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

Page 12: Exceptions

Throwable class inheritance hierarchy – a sample

Page 13: Exceptions

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.

Page 14: Exceptions

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.

Page 15: Exceptions

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

Page 16: Exceptions

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.

Page 17: Exceptions

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.

Page 18: Exceptions

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”);

}

Page 19: Exceptions

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.

Page 20: Exceptions

Catching Exceptions

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

Page 21: Exceptions

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

Page 22: Exceptions

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.

Page 23: Exceptions

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.

Page 24: Exceptions

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

Page 25: Exceptions

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.

Page 26: Exceptions

Example// 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; }

Page 27: Exceptions

Example continuedpublic 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);}

Page 28: Exceptions

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

Page 29: Exceptions

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.

Page 30: Exceptions

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.

Page 31: Exceptions

Calling a method that can throw an exception: two options

Page 32: Exceptions

Calling method that throws a runtime exception – 3 options