sheet 3 handling exceptions advanced programming using java by nora alaqeel
TRANSCRIPT
Sheet 3
Handling Exceptions
Advanced Programming using Java
By Nora Alaqeel
Java Programming: From Problem Analysis to Program Design, 4e
2
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
Java Programming: From Problem Analysis to Program Design, 4e
3
Handling Exception within a Program
Can use an if statement to handle an exceptionHowever, suppose that division by zero occurs in
more than one place within the same block 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
4
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
Java Programming: From Problem Analysis to Program Design, 4e
5
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
Java’s Mechanism of Exception Handling
Java Programming: From Problem Analysis to Program Design, 4e
6
Java Exception Hierarchy
Java Programming: From Problem Analysis to Program Design, 4e
7
Java Exception Hierarchy
Java Programming: From Problem Analysis to Program Design, 4e
8
Java Exception Hierarchy
Java Programming: From Problem Analysis to Program Design, 4e
9
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
Java Programming: From Problem Analysis to Program Design, 4e
10
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
Java Programming: From Problem Analysis to Program Design, 4e
11
try/catch/finally Block
Java Programming: From Problem Analysis to Program Design, 4e
12
try/catch/finally Block (continued)
•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
13
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
Java Programming: From Problem Analysis to Program Design, 4e
14
Order of catch Blocks
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
15
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
Java Programming: From Problem Analysis to Program Design, 4e
16
Unchecked Exceptions
Syntax:
ExceptionType1, ExceptionType2, and so on are names of exception classes
Java Programming: From Problem Analysis to Program Design, 4e
17
Exceptions Example Code
public static void exceptionMethod() throws InputMismatchException,FileNotFoundException
{ //statements}
The method exceptionMethod throws exceptions of the type InputMismatchException and FileNotFoundException
Java Programming: From Problem Analysis to Program Design, 4e
18
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
Java Programming: From Problem Analysis to Program Design, 4e
19
Rethrowing and Throwing an Exception
Syntaxthrow exceptionReference;
Java Programming: From Problem Analysis to Program Design, 4e
20
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());
} } }
Rethrowing and Throwing an Exception
Java Programming: From Problem Analysis to Program Design, 4e
21
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 ;} } }
Rethrowing and Throwing an Exception
Trace
By Nora Alaqeel