Академический Документы
Профессиональный Документы
Культура Документы
Handling
to
try,
throw,
catch
watch for
indicate
handle
exceptions
Motivation
We seek robust programs
When something unexpected occurs
Ensure program detects the problem
Then program must do something about it
Overview
Exception
Indication of problem during execution
Overview
Code that could generate errors put in try
blocks
Code for error handling enclosed in a catch
clause
The finally clause always executes
Exception Handler
Exception
"thrown" here
Thrown
Thrownexception
exceptionmatched
matchedagainst
against
first
firstset
setofofexception
exceptionhandlers
handlers
Exception
handler
Exception
handler
IfIfititfails
failsto
tomatch,
match,ititisismatched
matchedagainst
against
next
nextset
setofofhandlers,
handlers,etc.
etc.
IfIfexception
exceptionmatches
matchesnone
noneofofhandlers,
handlers,
program
programisisabandoned
abandoned
6
Terminology
Thrown exception an exception that has occurred
Stack trace
Name of the exception in a descriptive message that
indicates the problem
Complete method-call stack
Terminology
Throw point initial point at which the
exception occurs, top row of call chain
InputMismatchException occurs
when Scanner method nextInt receives
a string that does not represent a valid
integer
See Example, Figure 13.1
8
Termination Model of
Exception Handling
When an exception occurs:
try block terminates immediately
Program control transfers to first matching catch
block
Contrast
Termination model
program control does not return to the throw
point
try block has expired;
Flow of control proceeds to the first statement
after the last catch block
Resumption model
program control resumes just after throw point
10
11
12
13
Example: Handling
ArithmeticExceptions and
InputMismatchExceptions
With exception handling
program catches and handles the exception
14
unmatched catch
matching catch
unmatched catch
next step
15
unmatched catch
matching catch
unmatched catch
next step
16
Java Exception
Hierarchy
Superclass Throwable
Subclass Exception
Exceptional situations
Should be caught by program
Subclass Error
Typically not caught by program
Checked exceptions
Catch or declare
Unchecked exceptions
18
Java Exception
Hierarchy
All exceptions inherit either directly or
indirectly from class Exception
Exception classes form an inheritance
hierarchy that can be extended
19
Java Exception
Hierarchy
Class Throwable, superclass of Exception
Only Throwable objects can be used with the
exception-handling mechanism
21
Checked Exceptions
Inherit from class Exception but not from
RuntimeException
Compiler enforces catch-or-declare requirement
Compiler checks each method call and method
declaration
determines whether method throws checked
exceptions.
If so, the compiler ensures checked exception caught
or declared in throws clause.
If not caught or declared, compiler error 22
occurs.
Unchecked Exceptions
Inherit from class RuntimeException or class
Error
Compiler does not check code to see if
exception caught or declared
If an unchecked exception occurs and not
caught
Program terminates or runs with unexpected results
Java Exception
Hierarchy
catch block catches all exceptions of its
type and subclasses of its type
If there are multiple catch blocks that match
a particular exception type, only the first
matching catch block executes
Makes sense to use a catch block of a
superclass when all catch blocks for that
classs subclasses will perform same
functionality
24
finally Block
Consists of finally keyword followed by
a block of code enclosed in curly braces
Optional in a try statement
If present, is placed after the last catch
block
View position, Figure 13.4
25
finally Block
Executes whether or not an exception is
thrown in the corresponding try block or
any of its corresponding catch blocks
Will not execute if the application exits early
from a try block via method
System.exit
Typically contains resource-release code
26
Using finally
View program, Figure 13.5
Note
Re-throw of exception
Code for throw exception
Blocks using finally
Suggestion
Do not use a try block for every individual
statement which may cause a problem
Enclose groups of statements
27
Follow by multiple catch blocks
unmatched catch
matching catch
unmatched catch
finally
next step
28