Lecture7
Lecture7
Fall 2020/21
Object-Oriented Programming
CS-201, CS201, C212
Lec. (7)
Chapter 8
Exceptions
Objectives
After you have read and studied this chapter, you should be able to
Improve the reliability of code by incorporating exception-handling and assertion mechanisms.
Write methods that propagate exceptions.
Implement the try-catch blocks for catching and handling exceptions.
Write programmer-defined exception classes.
Distinguish the checked and unchecked, or runtime, exceptions.
Definition
An exception represents an error condition that can occur during the normal
course of program execution.
String inputStr;
int age;
try {
There are two methods we can call to get information about the thrown
exception:
getMessage
printStackTrace
try {
. . .
} catch (NumberFormatException e){
System.out.println(e.getMessage());
System.out.println(e.printStackTrace());
}
Multiple catch Blocks
A single try-catch statement can include multiple catch blocks, one
for each type of exception.
try {
. . .
age = Integer.parseInt(inputStr);
. . .
val = cal.get(id); //cal is a GregorianCalendar
. . .
} catch (NumberFormatException e){
. . .
} catch (ArrayIndexOutOfBoundsException e){
. . .
}
Multiple catch Control Flow
Exception No Exception
try { Assume <t-stmt-3> try {
throws an exception
<t-stmt-1> and <catch-block-3> <t-stmt-1>
<t-stmt-2> is the matching block. <t-stmt-2> All statements in
the try block are
<t-stmt-3> <t-stmt-3>
executed and throw
<t-stmt-4> Remaining <t-stmt-4> no exceptions.
. . . statements in the . . .
try block is skipped.
<t-stmt-n> <t-stmt-n>
} }
<catch-block-1> <catch-block-1>
<catch-block-2> Statements <catch-block-2> All catch
in the blocks are
<catch-block-3> <catch-block-3> skipped.
matching
. . . catch block . . .
<catch-block-m> are executed. <catch-block-m>
} }
<next stmt> <next stmt>
The finally Block
There are situations where we need to take certain actions regardless of whether
an exception is thrown or not.
We place statements that must be executed regardless of exceptions in the finally
block.
try-catch-finally Control Flow
Exception No Exception
try { Assume <t-stmt-i> try {
throws an exception
<t-stmt-1> <t-stmt-1>
and <catch-block-i> is
. . . the matching block. . . .
<t-stmt-i> <t-stmt-i>
. . . . . .
<t-stmt-n> <t-stmt-n>
} }
<catch-block-1> <catch-block-1>
. . . . . .
<catch-block-i> <catch-block-i>
. . . . . .
<catch-block-m> <catch-block-m>
} finally { } finally {
. . . finally block is . . . finally block is
} executed. } executed.
<next stmt> <next stmt>
Propagating Exceptions
Instead of catching a thrown exception by using the try-catch
statement, we can propagate the thrown exception back to the
caller of our method.
The method header includes the reserved word throws.
D
C C
B B B
A A A A
Stack Trace
Exception Types
All types of thrown errors are instances of the Throwable class or its sub
Serious errors are represented by instances of the Error class or its subclasses.
Exceptional cases that common applications should handle are represented by instanc
Exception class or its subclasses.
Throwable Hierarchy
When calling a method that can throw runtime exceptions, it is optional to use
the try-catch statement or modify the method header to include a throws clause.