Beruflich Dokumente
Kultur Dokumente
Lecture 6: Exceptions
Dr. L H!ng Ph"#ng -- Department of Mathematics, Mechanics and Informatics, VNUH
August 2012
1
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
Thursday, August 9, 12
What is an exception?
The exception object contains information about the error (its type,
the state of the program when the error occurred).
Thursday, August 9, 12
What is an exception?
Thursday, August 9, 12
What is an exception?
The runtime system searches the call stack for a method that can handle the
exception.
The block of code that handles the exception is called exception handler.
The type of exception object thrown must match the type that can be
handled by the handler.
When the handler is found, the runtime passes the exception to the handler.
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
Thursday, August 9, 12
Catching and handling exceptions
An example: ListOfNumbers
Thursday, August 9, 12
Catching and handling exceptions
public class ListOfNumbers {
public ListOfNumbers() {
list = new ArrayList<Integer>(SIZE);
for (int i = 0; i < SIZE; i++) {
list.add(new Integer(i));
}
}
Thursday, August 9, 12
Checked and unchecked
exceptions
The constructor new FileWriter("output.txt") initializes an output stream to a file.
There are two possible exceptions, however, the compiler reports only the
error message thrown by the FileWriter constructor.
Thursday, August 9, 12
The try block
Enclose the code that might throw an exception within a try block.
try {
// code
}
catch and finally blocks . . .
In our example:
PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("output.txt"));
for (int i = 0; i < SIZE; i++) {
out.println("Value at: " + i + " = " + list.get(i));
}
} catch ...
10
Thursday, August 9, 12
The catch blocks
Example: try {
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
throw new SampleException(e);
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} 11
Thursday, August 9, 12
The catch blocks
In Java SE 7 and later, a single catch block can handle multiple types of
exception.
catch (IOException | SQLException ex) {
logger.log(ex);
throw ex;
}
12
Thursday, August 9, 12
The finally block
The finally block always executes when the try block exits.
In the example, the program should close the stream before exiting the
writeList() method. The try block can exit in one of three ways:
Thursday, August 9, 12
The finally block
14
Thursday, August 9, 12
The complete example
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
} finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}
} 15
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
16
Thursday, August 9, 12
Specifying the exceptions thrown
by a method
We have seen how exceptions can be handled by a method.
Sometimes, its better to let a method further up the call stack handle
the exceptions.
In this case, the method must specify that it can throw the exceptions
that could occur within its code.
public void writeList() throws IOException, ArrayIndexOutOfBoundsException {
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
18
Thursday, August 9, 12
How to throw exceptions
19
Thursday, August 9, 12
Throwable class and its subclasses
Failures
of the JRE
20
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
21
Thursday, August 9, 12
Advantages of exceptions
Thursday, August 9, 12
Advantages of exceptions
Thursday, August 9, 12
errorCodeType readFile {
initialize errorCode = 0;
Thursday, August 9, 12
errorCodeType readFile { readFile {
initialize errorCode = 0; try {
open the file;
open the file; determine its size;
if (theFileIsOpen) { allocate that much memory;
determine the length of the file; read the file into memory;
if (gotTheFileLength) { close the file;
allocate that much memory; } catch (fileOpenFailed) {
if (gotEnoughMemory) { doSomething;
} catch (sizeDeterminationFailed) {
read the file into memory;
doSomething;
if (readFailed) {
} catch (memoryAllocationFailed) {
errorCode = -1; doSomething;
} } catch (readFailed) {
} else { doSomething;
errorCode = -2; } catch (fileCloseFailed) {
} doSomething;
} else { }
errorCode = -3; }
}
close the file;
if (theFileDidntClose && errorCode == 0) {
errorCode = -4;
} else {
errorCode = errorCode and -4;
}
} else {
errorCode = -5;
}
return errorCode;
}
23
Thursday, August 9, 12
Advantages of exceptions
method2 {
call method3;
}
method3 {
call readFile;
}
24
Thursday, August 9, 12
Advantages of exceptions
Suppose that the method1 is the only method interested in the error
that might occur within readFile.
25
Thursday, August 9, 12
Advantages of exceptions
In Java, only the methods that care about errors have to worry about
detecting errors.
method1 {
try {
call method2;
} catch (exception e) {
doErrorProcessing;
}
}
Thursday, August 9, 12
Advantages of exceptions
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException: "
+ e.getMessage());
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
27
Thursday, August 9, 12
Content
What is an exception?
Advantages of exceptions
Exercises
28
Thursday, August 9, 12
Exercises
Exercise 1. Write the program which was asked in the midterm test.
Note the use of exceptions in your code.
} catch (Exception e) {
} catch (ArithmeticException a) {
}
29
Thursday, August 9, 12