7.2 Catching an exception
One of the advantages of Java exception handling is
that it allows you to concentrate on the problem you're trying to solve
in one place, and then deal with the errors from that code in another place.
7.2.1 The try block
A special block within that method to capture the exception.
try {
// Code that might generate exception
}
7.2.2 Exception handlers
Exception handlers immediately follow the try block
and are denoted by the keyword catch:
try {
// Code that might generate exception
} catch ( Type1 id1 ) {
// Handle exceptions of Type1
} catch ( Type2 id2 ) {
// Handle exceptions of Type2
}
Each catch clause is like a little method that takes
one and only one argument of a particular type.
There're two basic models in exception-handling theory:
-
termination: The error is so critical that there's no way
to go back.
-
resumption: The exception handler is expected to do something
to rectify the situation, and then faulting method is retried.