Java Reference
In-Depth Information
•Multi- catch (p. 449) enables you to catch multiple exception types in a single catch handler and
perform the same task for each type of exception. The syntax for a multi- catch is:
catch ( Ty p e 1 | Ty p e 2 | Ty p e 3 e)
• Each exception type is separated from the next with a vertical bar ( | ).
• If an exception occurs in a try block, the try block terminates immediately and program control
transfers to the first catch block with a parameter type that matches the thrown exception's type.
• After an exception is handled, program control does not return to the throw point, because the
try block has expired. This is known as the termination model of exception handling (p. 449).
• If there are multiple matching catch blocks when an exception occurs, only the first is executed.
•A throws clause (p. 450) specifies a comma-separated list of exceptions that the method might
throw, and appears after the method's parameter list and before the method body.
Section 11.4 When to Use Exception Handling
• Exception handling processes synchronous errors (p. 451), which occur when a statement executes.
• Exception handling is not designed to process problems associated with asynchronous events
(p. 451), which occur in parallel with, and independent of, the program's flow of control.
Section 11.5 Java Exception Hierarchy
• All Java exception classes inherit directly or indirectly from class Exception .
• Programmers can extend the Java exception hierarchy with their own exception classes.
• Class Throwable is the superclass of class Exception and is therefore also the superclass of all ex-
ceptions. Only Throwable objects can be used with the exception-handling mechanism.
• Class Throwable (p. 451) has two subclasses: Exception and Error .
• Class Exception and its subclasses represent problems that could occur in a Java program and be
caught by the application.
• Class Error and its subclasses represent problems that could happen in the Java runtime system.
Error s happen infrequently and typically should not be caught by an application.
• Java distinguishes between two categories of exceptions (p. 452): checked and unchecked.
• The Java compiler does not check to determine if an unchecked exception is caught or declared.
Unchecked exceptions typically can be prevented by proper coding.
• Subclasses of RuntimeException represent unchecked exceptions. All exception types that inherit
from class Exception but not from RuntimeException (p. 452) are checked.
•If a catch block is written to catch exception objects of a superclass type, it can also catch all ob-
jects of that class's subclasses. This allows for polymorphic processing of related exceptions.
Section 11.6 finally Block
• Programs that obtain certain types of resources must return them to the system to avoid so-called
resource leaks (p. 454). Resource-release code typically is placed in a finally block (p. 454).
•The finally block is optional. If it's present, it's placed after the last catch block.
•The finally block will execute whether or not an exception is thrown in the corresponding try
block or any of its corresponding catch blocks.
• If an exception cannot be caught by one of that try block's associated catch handlers, control
proceeds to the finally block. Then the exception is passed to the next outer try block.
•If a catch block throws an exception, the finally block still executes. Then the exception is
passed to the next outer try block.
Search WWH ::




Custom Search