Section 10.8 Chapter Summary
Subsection 10.8.1 Technical Terms
catch block | catch an exception | checked exception |
dialog box | dynamic scope | error dialog |
exception | exception handling | finally block |
method call stack | method stack trace | modal dialog |
static scope | throw an exception | try block |
unchecked exception |
Subsection 10.8.2 The Try/Catch Statement
The
try/catch/finally
statement has the following syntax:try {
// Block of statements
// At least one of which may throw an exception
if ( * Some condition obtains */ )
throw new ExceptionName();} catch (ExceptionName ParameterName) {
// Block of statements to be executed
// If the ExceptionName exception is thrown in try}
..} catch (ExceptionName2 ParameterName) {
// Block of statements to be executed
// If the ExceptionName2 exception is thrown in try
} finally {
// Optional block of statements that is executed
// Whether an exception is thrown or not
}
The try block is meant to include a statement or statements that might throw an exception. The catch blocks—there can be one or more—are meant to handle exceptions that are thrown in the try block. A catch block will handle any exception that matches its parameter class, including subclasses of that class. The finally block is optional. It will be executed whether an exception is thrown or not. If an exception is thrown in the try block, the try block is exited permanently.
The
throw
statement inside the try block is there to illustrate how throw
can be used. You will usually not see a throw
statement in a try block, because most throws are done from within Java library methods, which are called from a try
block.Subsection 10.8.3 Summary of Important Points
- In Java, when an error or exceptional condition occurs, you
throw
anException
, which is caught by special code known as an exception handler. Athrow
statement—throw new Exception()
—is used to throw an exception. - A try block is a block of statements containing one or more statements that may throw an exception. Embedding a statement in a try block indicates your awareness that it might throw an exception and your intention to handle the exception.
- Java distinguishes between checked and unchecked exceptions. Checked exceptions must either be caught by the method in which they occur or you must declare that the method containing that statement
throws
the exception. - Unchecked exceptions are those that belong to subclasses of
RuntimeException
. If they are left uncaught, they will be handled by Java’s default exception handlers. - A catch block is a block of statements that handles the exceptions that match its parameter. A catch block can only follow a try block, and there may be more than one catch block for each try block.
- The
try/catch
syntax allows you to separate the normal parts of an algorithm from special code meant to handle errors and exceptional conditions. - A method stack trace is a trace of the method calls that have led to the execution of a particular statement in the program. The
Exception.printStackTrace()
method can be called by exception handlers to print a trace of exactly how the program reached the statement that threw the exception. Static scoping refers to how the text of the program is arranged. If a variable is declared within a method or a block, its static scope is confined to that method or block. Dynamic scoping refers to how the program is executed. A statement is within the dynamic scope of a method or block if it is called from that method or block, or if it is called by some other method that was called from that method or block. - When searching for a catch block to handle an exception thrown by a statement, Java searches upward through the statement’s static scope and backward through its dynamic scope until it finds a matching catch block. If none is found, the Java Virtual Machine will handle the exception itself by printing an error message and a method stack trace.
- Many Java library methods throw exceptions when an error occurs. These
throw
statements do not appear in the program. For example, Java’s integer division operator will throw anArithmeticException
if an attempt is made to divide by zero. - Generally, there are four ways to handle an exception: (1) Let Java handle it; (2) fix the problem that led to the exception and resume the program; (3) report the problem and resume the program; and (4) print an error message and terminate the program. Most erroneous conditions reported by exceptions are difficult or impossible to fix.
- A
finally
statement is an optional part of atry/catch
block. Statements contained in a finally block will be executed whether an exception is raised or not. - A well-designed program should use exception handling to deal with truly exceptional conditions, not as a means of normal program control.
- User-defined exceptions can be defined by extending the
Exception
class or one of its subclasses.
Solutions 10.8.4 Solutions to Self-Study Exercises
10.3 Java’s Exception Hierarchy
10.3.1 Java Predefined Exceptions
Self-Study Exercise
10.3.6 Self-Study Exercises
Exercise 10.3.8. Unchecked Exceptions.
10.4 Handling Exceptions Within a Program
10.4.7 Self-Study Exercises
10.4.7.1. Catches.
10.4.7.2. Exception Output.
10.4.7.3. Exception Output 2.
10.4.7.4. Divide by Zero Exception.
10.4.7.5. Modify Divide by Zero.
10.4.7.6. Exception Output 3.
10.4.7.7. Exception Output 4.
10.4.7.8. Try/Catch Block.
10.5 Error Handling and Robust Program Design
10.5.5 Self-Study Exercise
Exercise 10.5.19. Resume or Terminate.
10.6 Creating and Throwing Your Own Exceptions
10.6.2 IntFieldClass
Self-Study Exercises
10.6.2.1. FieldIsEmptyException.
10.6.2.2. IntField.getInt().
You have attempted of activities on this page.