Professional Documents
Culture Documents
Chapter 6
Exception Handling
By: Mequanent Argaw
Debre Markos University
1 Department of ECE, College of Technology 12/21/2015
Lecture Outline
Exception Handling
Exception Types
Hardware failures
Example:
java.lang.ArrayIndexOutOfBoundsException: 2
at exceptionexample.ExceptionExample.main
(ExceptionExample.java:12)
What is the exception class? ArrayIndexOutOfBoundsException
Which array index is out of bounds? 2
What method throws the exception? main
What file contains the method? ExceptionExample.java
What line of the file throws the exception? 12
5 Department of ECE, College of Technology 12/21/2015
Exception Handling
try {
// code that might throw exception
}
catch ([Type of Exception] e) {
// what to do if exception is thrown
}
Unchecked Exceptions
programmer.
10 Department of ECE, College of Technology 12/21/2015
Checked Exceptions
There are also Checked Exceptions
control:
examples: hardware failures, unreadable files
the programmer . . .
ArrayIndexOutofBounds FileNotFoundException
NullPointerException MalformedURLException
IllegalArgumentException SocketException
etc. etc.
If not caught, method must specify it If not caught, method may specify it
to be thrown. to be thrown.
Errors that the programmer cannot Errors that the programmer can
directly prevent from occurring. directly prevent from occurring.
IOException, NullPointerException,
FileNotFoundException, IllegalArgumentException,
SocketException IllegalStateException
1. no arguments
NullPointerException e = new NullPointerException();
may be thrown.
throws – to specify which exceptions a method
intersect method: