Chapter 7 Exceptions
Chapter 7 Exceptions
El Dick 1
Introduction
2
EXCEPTIONS
Chapter 7 M. El Dick
Running some class Square : Any Java method can “throw” an Object
object of class Throwable
Enter an integer: rats
Exception in thread "main" java.util.InputMismatchException Programs can recover from Throwable
at java.util.Scanner.throwFor(Unknown Source) Exceptions
at java.util.Scanner.next(Unknown Source) Can “catch” Exceptions
extend
at java.util.Scanner.nextInt(Unknown Source) Exception Error
Programs cannot recover from
at java.util.Scanner.nextInt(Unknown Source)
Errors
at Square.main(Square.java:12)
M. El Dick M. El Dick
Object
Throwable
InputMismatchException
6
import java.util.* ;
public class Square
M. El Dick 10 M. El Dick
yes
Is it correct? How try and catch Work
11 12
try
{ Do not have to catch all
// various statements possible exceptions
}
Some type of exception
catch (InputMismatchException ex )
{
// various statements to handle this type of
exception
}
catch (IOException ex )
Another type of exception
{
// various statements to handle this type of
exception
M. El Dick M. El Dick
}
Types of Exception Types of Exception
13 14
1st matched catch{} gets control In catch{} blocks, a child class should
appear before any of its ancestors
Exception
InputMismatchException
M. El Dick M. El Dick
import java.util.* ;
M. El Dick M. El Dick
… Output
try{
System.out.print("Enter the numerator: ");
num = scan.nextInt(); Enter the numerator: 13
System.out.print("Enter the divisor : "); Enter the divisor : 4
div = scan.nextInt(); 13 / 4 is 3 rem 1
System.out.println( num + " / " + div + " is " + (num/div) + " rem " + If something went wrong, you entered bad data.
(num%div) );
Goodbye
}
catch (ArithmeticException ex ){
System.out.println("You can't divide " + num + " by " + div);
} Enter the numerator: rats
finally If something went wrong, you entered bad data.
{ Exception in thread "main"
System.out.println("If something went wrong, you entered bad data." ); java.util.InputMismatchException
} at java.util.Scanner.throwFor(Unknown Source)
} at java.util.Scanner.next(Unknown Source)
System.out.println("Goodbye" );
at java.util.Scanner.nextInt(Unknown Source)
}
at java.util.Scanner.nextInt(Unknown Source)
at FinallyPractice.main(FinallyPractice.java:13)
21 M. El Dick 22 M. El Dick
Output
M. El Dick M. El Dick
import java.util.* ;
Output
public class IndexPractice {
public static void main ( String[] a ) { Enter the data: 8
Scanner scan = new Scanner( System.in ); Enter the array index: 10
int data=0, slot=0 ; This is your problem: 10
int[] value = new int[10];
try { Here is where it happened:
System.out.print("Enter the data: "); java.lang.ArrayIndexOutOfBoundsException: 10
data = scan.nextInt(); at IndexPractice.main(IndexPractice.java:18)
System.out.print("Enter the array index: "); Goodbye
slot = scan.nextInt();
value[slot] = data;
}
catch (InputMismatchException ex ) {
System.out.println("This is your problem: " + ex.getMessage() + "\n
Here is where it happened:\n"); Line number in the program where the exception
ex.printStackTrace(); occured.
}
catch (IndexOutOfBoundsException ex ) {
System.out.println("This is your problem: " + ex.getMessage() + "\n
Here is where it happened:\n");
ex.printStackTrace();
}
System.out.println("Goodbye" );
27
}} M. El Dick 28 M. El Dick
Checked Exceptions Checked and Unchecked Exceptions
29 30
Exception CHECKED
Method must do something about them : IOException CHECKED
handle the exception in a catch{} block, or AWTException CHECKED
throw the exception to the caller of the method RunTimeException
ArithmeticException
IllegalArgumentException
Compiler checks to make sure that this is done NumberFormatException
IndexOutOfBoundsException
NoSuchElementException
InputMismatchException
Others
Others CHECKED
M. El Dick M. El Dick
M. El Dick M. El Dick
import java.util.* ;
M. El Dick M. El Dick
Exercise Exercise (cont’d)
41 42
M. El Dick M. El Dick
Note
43
M. El Dick