Exception Handling in Java: Richard S. Huntrods June 14, 2001 University of Calgary
Exception Handling in Java: Richard S. Huntrods June 14, 2001 University of Calgary
Richard S. Huntrods
June 14, 2001
University of Calgary
Exception Handling in Java
Topics:
Introduction
Errors and Error handling
Exceptions
Types of Exceptions
Coding Exceptions
Summary
Exception Handling in Java
Introduction
Introduction
Exceptions
An exception is a representation of an
error condition or a situation that is not
the expected result of a method.
Exceptions are built into the Java
language and are available to all
program code.
Exceptions isolate the code that deals
with the error condition from regular
program logic.
Exception Handling in Java
Exceptions
Checked Exceptions
Unchecked Exceptions
10
Exceptions
11
Exceptions
Examples:
12
Exceptions
13
Exceptions
To handle the exception, you write a trycatch block. To pass the exception up the
chain, you declare a throws clause in your
method or class declaration.
If the method contains code that may cause
a checked exception, you MUST handle the
exception OR pass the exception to the
parent class (remember, every class has
Object as the ultimate parent)
Exception Handling in Java
14
Coding Exceptions
Try-Catch Mechanism
15
Coding Exceptions
Try-Catch Mechanism
16
Coding Exceptions
Example
try {
normal program code
}
catch(Exception e) {
exception handling code
}
17
Coding Exceptions
18
Coding Exceptions
Types of Exceptions
19
Coding Exceptions
Types of Exceptions
Examples:
20
Summary
21