Transcript sh3

Advanced Programming using
Java
Sheet 3
HANDLING EXCEPTIONS
By Nora Alaqeel
Exception
 Definition: an occurrence of an undesirable situation
that can be detected during program execution
 Examples
 Division by zero
 Invalid input errors
 An array index that goes out of bounds
2
Java Programming: From Problem
Analysis to Program Design, 4e
Handling Exception within a Program
 Can use an if statement to handle an exception
 However, suppose that division by zero occurs in
more than one place within the same block

3
In this case, using if statements may not be the most effective
way to handle the exception
Java Programming: From Problem
Analysis to Program Design, 4e
Java’s Mechanism of Exception Handling
 Java provides a number of exception classes to effectively
handle certain common exceptions such as division by
zero, invalid input, and file not found
 When an exception occurs, an object of a particular
exception class is created
4
Java Programming: From Problem
Analysis to Program Design, 4e
Java’s Mechanism of Exception Handling
 When a division by zero exception occurs, the
program creates an object of the class
ArithmeticException
 When a Scanner object is used to input data into a
program, any invalid input errors are handled using
the class InputMismatchException
 The class Exception is the superclass of all the
exception classes in Java
5
Java Programming: From Problem
Analysis to Program Design, 4e
Java Exception Hierarchy
6
Java Programming: From Problem Analysis to
Program Design, 4e
Java Exception Hierarchy
7
Java Programming: From Problem Analysis to
Program Design, 4e
Java Exception Hierarchy
8
Java Programming: From Problem Analysis to
Program Design, 4e
try/catch/finally Block
 Statements that might generate an exception are placed in
a try block
 The try block might also contain statements that should
not be executed if an exception occurs
 The try block is followed by zero or more catch
blocks
 A catch block specifies the type of exception it can
catch and contains an exception handler
9
Java Programming: From Problem
Analysis to Program Design, 4e
try/catch/finally Block
 The last catch block may or may not be followed
by a finally block
 Any code contained in a finally block always
executes, regardless of whether an exception occurs,
except when the program exits early from a try
block by calling the method System.exit
 If a try block has no catch block, then it must
have the finally block
10
Java Programming: From Problem
Analysis to Program Design, 4e
try/catch/finally Block
11
Java Programming: From Problem Analysis to
Program Design, 4e
try/catch/finally Block
(continued)
12
If no exception is thrown in a try block, all •
catch blocks associated with the try block are
ignored and program execution resumes after the
last catch block
If an exception is thrown in a try block, the •
remaining statements in the try block are ignored
- The program searches the catch blocks in the order
in which they appear after the try block and looks for
an appropriate exception handler
Java Programming: From Problem Analysis to
Program Design, 4e
try/catch/finally Block
•
If the type of the thrown exception matches the
parameter type in one of the catch blocks, the
code of that catch block executes and the
remaining catch blocks after this catch block
are ignored
•
If there is a finally block after the last
catch block, the finally block executes
regardless of whether an exception occurs
13
Java Programming: From Problem
Analysis to Program Design, 4e
Order of catch Blocks
14
 The heading of a catch block specifies the type of
exception it handles.
 If in the heading of a catch block you declare an
exception using the class Exception, then that
catch block can catch all types of exceptions because
the class Exception is the superclass of all
exception classes
Java Programming: From Problem Analysis to
Program Design, 4e
Exception-Handling Techniques
 Terminate program
 Output appropriate error message upon termination.
 Log error and continue
 Display error messages and continue with program execution
 Fix error and continue
 Repeatedly get user input
 Output appropriate error message until valid value is entered
15
Java Programming: From Problem
Analysis to Program Design, 4e
Unchecked Exceptions
Syntax:
 ExceptionType1, ExceptionType2, and so on are
names of exception classes
16
Java Programming: From Problem
Analysis to Program Design, 4e
Exceptions Example Code
public static void exceptionMethod() throws
InputMismatchException,FileNotFoundException
{
//statements
}
 The method exceptionMethod throws exceptions of the
type InputMismatchException and
FileNotFoundException
17
Java Programming: From Problem
Analysis to Program Design, 4e
Rethrowing and Throwing an Exception
 When an exception occurs in a try block, control
immediately passes to one of the catch blocks; typically,
a catch block does one of the following:
Completely handles the exception.
 Rethrows the same exception for the calling environment to
handle the exception.
 Partially processes the exception; in this case, the catch block
either rethrows the same exception or throws another exception
for the calling environment to handle the exception

18
Java Programming: From Problem
Analysis to Program Design, 4e
Rethrowing and Throwing an Exception
 Syntax
throw exceptionReference;
19
Java Programming: From Problem
Analysis to Program Design, 4e
Rethrowing and Throwing an Exception
import java.util.*;
public class RethrowExceptionExmp1
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int number;
try
{
number = getNumber();
System.out.println("Line 5: number = ”+ number);
}
catch (InputMismatchException ex)
{
System.out.println("Line 7: Exception ” +
ex.toString());
}
}
}
20
Java Programming: From Problem
Analysis to Program Design, 4e
Rethrowing and Throwing an Exception
public static int getNumber()throws
InputMismatchException
{
int num;
try
{
System.out.print(“Line 11: Enter an integer: ");
num = console.nextInt();
System.out.println();
return num;
}
catch (InputMismatchException e)
{
throw e;
}
}
}
21
Java Programming: From Problem
Analysis to Program Design, 4e
Trace
By Nora Alaqeel