Java Reference
In-Depth Information
• You can allocate multiple resources in the parentheses following
try
by separating them with a
semicolon (
;
).
Self-Review Exercises
11.1
List five common examples of exceptions.
11.2
Why are exceptions particularly appropriate for dealing with errors produced by methods
of classes in the Java API?
11.3
What is a “resource leak”?
11.4
If no exceptions are thrown in a
try
block, where does control proceed to when the
try
block completes execution?
11.5
Give a key advantage of using
catch(Exception
exceptionName
)
.
11.6
Should a conventional application catch
Error
objects? Explain.
11.7
What happens if no
catch
handler matches the type of a thrown object?
11.8
What happens if several
catch
blocks match the type of the thrown object?
11.9
Why would a programmer specify a superclass type as the type in a
catch
block?
11.10
What is the key reason for using
finally
blocks?
11.11
What happens when a
catch
block throws an
Exception
?
11.12
What does the statement
throw
exceptionReference
do in a
catch
block?
11.13
What happens to a local reference in a
try
block when that block throws an
Exception
?
Answers to Self-Review Exercises
11.1
Memory exhaustion, array index out of bounds, arithmetic overflow, division by zero, in-
valid method parameters.
11.2
It's unlikely that methods of classes in the Java API could perform error processing that
would meet the unique needs of all users.
11.3
A “resource leak” occurs when an executing program does not properly release a resource
when it's no longer needed.
11.4
The
catch
blocks for that
try
statement are skipped, and the program resumes execution
after the last
catch
block. If there's a
finally
block, it's executed first; then the program resumes
execution after the
finally
block.
11.5
The form
catch(Exception
exceptionName
) catches any type of exception thrown in a
try
block. An advantage is that no thrown
Exception
can slip by without being caught. You can handle
the exception or rethrow it.
11.6
Error
s are usually serious problems with the underlying Java system; most programs will
not want to catch
Error
s because they will not be able to recover from them.
11.7
This causes the search for a match to continue in the next enclosing
try
statement. If there's
a
finally
block, it will be executed before the exception goes to the next enclosing
try
statement.
If there are no enclosing
try
statements for which there are matching
catch
blocks and the excep-
tions are declared (or unchecked), a stack trace is printed and the current thread terminates early. If
the exceptions are checked, but not caught or declared, compilation errors occur.
11.8
The first matching
catch
block after the
try
block is executed.