Java Reference
In-Depth Information
cussed checked vs. unchecked exceptions, and how to specify with the throws clause the
exceptions that a method might throw.
You learned how to use the finally block to release resources whether or not an
exception occurs. You also learned how to throw and rethrow exceptions. We showed how
to obtain information about an exception using methods printStackTrace , getStack-
Trace and getMessage . Next, we presented chained exceptions, which allow you to wrap
original exception information with new exception information. Then, we showed how to
create your own exception classes.
We introduced preconditions and postconditions to help programmers using your
methods understand conditions that must be true when the method is called and when it
returns, respectively. When preconditions and postconditions are not met, methods typi-
cally throw exceptions. We discussed the assert statement and how it can be used to help
you debug your programs. In particular, assert can be used to ensure that preconditions
and postconditions are met.
We also introduced multi- catch for processing several types of exceptions in the same
catch handler and the try -with-resources statement for automatically deallocating a
resource after it's used in the try block. In the next chapter, we take a deeper look at graph-
ical user interfaces (GUIs).
Summary
Section 11.1 Introduction
• An exception is an indication of a problem that occurs during a program's execution.
• Exception handling enables programmers to create applications that can resolve exceptions.
Section 11.2 Example: Divide by Zero without Exception Handling
• Exceptions are thrown (p. 443) when a method detects a problem and is unable to handle it.
• An exception's stack trace (p. 444) includes the name of the exception in a message that indicates
the problem that occurred and the complete method-call stack at the time the exception occurred.
• The point in the program at which an exception occurs is called the throw point (p. 445).
Section 11.3 Example: Handling ArithmeticException s and InputMismatchEx-
ception s
•A try block (p. 447) encloses code that might throw an exception and code that should not ex-
ecute if that exception occurs.
• Exceptions may surface through explicitly mentioned code in a try block, through calls to other
methods or even through deeply nested method calls initiated by code in the try block.
•A catch block (p. 448) begins with keyword catch and an exception parameter followed by a block
of code that handles the exception. This code executes when the try block detects the exception.
• At least one catch block or a finally block (p. 448) must immediately follow the try block.
•A catch block specifies in parentheses an exception parameter identifying the exception type to
handle. The parameter's name enables the catch block to interact with a caught exception object.
• An uncaught exception (p. 449) is an exception that occurs for which there are no matching
catch blocks. An uncaught exception will cause a program to terminate early if that program con-
tains only one thread. Otherwise, only the thread in which the exception occurred will terminate.
The rest of the program will run but possibly with adverse results.
Search WWH ::




Custom Search