Java Reference
In-Depth Information
ȗ
A NoSuchElementException is not caught by any of the catch clauses.
The exception remains thrown until it is caught by another try block.
When the catch (IOException exception) block is executed, then some
method in the try block has failed with an IOException . The variable
exception contains a reference to the exception object that was thrown. The
catch clause can analyze that object to find out more details about the failure. For
example, you can get a printout of the chain of method calls that lead to the
exception, by calling
exception.printStackTrace()
In these sample catch clauses, we merely inform the user of the source of the
problem. A better way of dealing with the exception would be to give the user another
chance to provide a correct inputȌsee Section 11.7 for a solution.
It is important to remember that you should place catch clauses only in methods in
which you can competently handle the particular exception type.
S ELF C HECK
7.
Suppose the file with the given file name exists and has no contents.
Trace the flow of execution in the try block in this section.
8.
Is there a difference between catching checked and unchecked
exceptions?
Q UALITY T IP 11.1
Throw Early, Catch Late
When a method notices a problem that it cannot solve, it is generally better to
throw an exception rather than try to come up with an imperfect fix (such as doing
nothing or returning a default value).
It is better to declare that a method throws a checked exception than to handle
the exception poorly.
Search WWH ::




Custom Search