Professional Documents
Culture Documents
Chapter 17 Exceptions and Assertions
Chapter 17 Exceptions and Assertions
Assertions
Chapter 9 Inheritance and Polymorphism
Chapter 17 Exceptions and Assertions
Chapter 18 Binary I/O
Chapter 6 Arrays
Chapter 19 Recursion
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Objectives
Runtime Errors
1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;
}
Terminated.
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
13
import java.util.*;
Run
}
catch (InputMismatchException ex) {
System.out.println("Try again. (" +
"Incorrect input: an integer is required)");
scanner.nextLine(); // discard input
}
} while (continueInput);
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Exception Classes
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
System Errors
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Exceptions
Exception describes errors
caused by your program
and external
circumstances. These
errors can be caught and
handled by your program.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Runtime Exceptions
RuntimeException is caused by
programming errors, such as bad
casting, accessing an out-of-bounds
array, and numeric errors.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
10
Unchecked Exceptions
In most cases, unchecked exceptions reflect programming
logic errors that are not recoverable. For example, a
NullPointerException is thrown if you access an object
through a reference variable before an object is assigned to
it; an IndexOutOfBoundsException is thrown if you access
an element in an array outside the bounds of the array.
These are the logic errors that should be corrected in the
program. Unchecked exceptions can occur anywhere in the
program. To avoid cumbersome overuse of try-catch
blocks, Java does not mandate you to write code to catch
unchecked exceptions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
11
Unchecked
exception.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
12
method1() {
catch exception
}
try {
invoke method2;
}
catch (Exception ex) {
Process exception;
}
declare exception
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
throw exception
13
Declaring Exceptions
Every method must state the types of checked
exceptions it might throw. This is known as
declaring exceptions.
public void myMethod()
throws IOException
public void myMethod()
throws IOException, OtherException
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
14
Throwing Exceptions
When the program detects an error, the program
can create an instance of an appropriate exception
type and throw it. This is known as throwing an
exception. Here is an example,
throw new TheException();
TheException ex = new TheException();
throw ex;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
15
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
16
Catching Exceptions
try {
statements; // Statements that may throw exceptions
}
catch (Exception1 exVar1) {
handler for exception1;
}
catch (Exception2 exVar2) {
handler for exception2;
}
...
catch (ExceptionN exVar3) {
handler for exceptionN;
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
17
Catching Exceptions
main method {
...
try {
...
invoke method1;
statement1;
}
catch (Exception1 ex1) {
Process ex1;
}
statement2;
}
method1 {
...
try {
...
invoke method2;
statement3;
}
catch (Exception2 ex2) {
Process ex2;
}
statement4;
}
method2 {
...
try {
...
invoke method3;
statement5;
}
catch (Exception3 ex3) {
Process ex3;
}
statement6;
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
An exception
is thrown in
method3
18
(b)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
19
CircleWithException
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
20
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
21
IntegerDivision
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
22
Rethrowing Exceptions
try {
statements;
}
catch(TheException ex) {
perform operations before exits;
throw ex;
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
23
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
24
animation
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
25
animation
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
26
animation
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement in
the method is
executed
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
27
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Suppose an exception
of type Exception1 is
thrown in statement2
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
28
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
The exception is
handled.
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
29
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
30
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
31
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
statement2 throws an
exception of type
Exception2.
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
32
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Handling exception
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
33
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
34
animation
try {
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
35
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
36
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
37
38
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
39
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
40
TestCircleWithRadiusException
Run
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
41
Assertions
An assertion is a Java statement that enables
you to assert an assumption about your
program. An assertion contains a Boolean
expression that should be true during program
execution. Assertions can be used to assure
program correctness and avoid logic errors.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
42
Declaring Assertions
An assertion is declared using the new Java keyword
assert in JDK 1.4 as follows:
assert assertion; or
assert assertion : detailMessage;
where assertion is a Boolean expression and
detailMessage is a primitive-type or an Object value.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
43
Executing Assertions
When an assertion statement is executed, Java evaluates the
assertion. If it is false, an AssertionError will be thrown. The
AssertionError class has a no-arg constructor and seven
overloaded single-argument constructors of type int, long, float,
double, boolean, char, and Object.
For the first assert statement with no detail message, the no-arg
constructor of AssertionError is used. For the second assert
statement with a detail message, an appropriate AssertionError
constructor is used to match the data type of the message. Since
AssertionError is a subclass of Error, when an assertion becomes
false, the program displays a message on the console and exits.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
44
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
45
46
47
48
49
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
50
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
51