System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə43/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   39   40   41   42   43   44   45   46   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

handler, that is executed automatically when an error occurs. It is not necessary to
manually check the success or failure of each specific operation or method call. If an
error occurs, it will be processed by the exception handler.
Another reason that exception handling is important is that Java defines standard
exceptions for common program errors, such as divide­by­zero or file­not­found. To
respond to these errors, your program must watch for and handle these exceptions.
Also, Java’s API library makes extensive use of exceptions.
In the final analysis, to be a successful Java programmer means that you are fully
capable of navigating Java’s exception handling subsystem.
THE EXCEPTION HIERARCHY


In Java, all exceptions are represented by classes. All exception classes are derived from
a class called Throwable. Thus, when an exception occurs in a program, an object of
some type of exception class is generated. There are two direct subclasses of
Throwable: Exception and Error. Exceptions of type Error are related to errors
that occur in the Java Virtual Machine itself, and not in your program. These types of
exceptions are beyond your control, and your program will not usually deal with them.
Thus, these types of exceptions are not described here.
Errors that result from program activity are represented by subclasses of Exception.
For example, divide­by­zero, array boundary, and file errors fall into this category. In
general, your program should handle exceptions of these types. An important subclass
of Exception is RuntimeException, which is used to represent various common
types of run­time errors.
EXCEPTION HANDLING FUNDAMENTALS
Java exception handling is managed via five keywords: try, catch, throw, throws,
and finally. They form an interrelated subsystem in which the use of one implies the
use of another. Throughout the course of this chapter, each keyword is examined in
detail. However, it is useful at the outset to have a general understanding of the role
each plays in exception handling. Briefly, here is how they work.
Program statements that you want to monitor for exceptions are contained within a try
block. If an exception occurs within the try block, it is thrown. Your code can catch this
exception using catch and handle it in some rational manner. System­generated
exceptions are automatically thrown by the Java run­time system. To manually throw
an exception, use the keyword throw. In some cases, an exception that is thrown out of
a method must be specified as such by a throws clause. Any code that absolutely must
be executed upon exiting from a try block is put in a finally block.
Ask the Expert
Q
: Just to be sure, could you review the conditions that cause an
exception to be generated?
A
: Exceptions are generated in three different ways. First, the Java Virtual
Machine can generate an exception in response to some internal error which is
beyond your control. Normally, your program won’t handle these types of


exceptions. Second, standard exceptions, such as those corresponding to divide­by­
zero or array index out­of­bounds, are generated by errors in program code. You
need to handle these exceptions. Third, you can manually generate an exception by
using the throw statement. No matter how an exception is generated, it is handled
in the same way.
Using try and catch
At the core of exception handling are try and catch. These keywords work together;
you can’t have a catch without a try. Here is the general form of the try/catch
exception handling blocks:
try {
// block of code to monitor for errors
}
catch (ExcepType1 exOb) {
// handler for ExcepType1
}
catch (ExcepType2 exOb) {
// handler for ExcepType2
}
.
.
.
Here, ExcepType is the type of exception that has occurred. When an exception is
thrown, it is caught by its corresponding catch statement, which then processes the
exception. As the general form shows, there can be more than one catch statement
associated with a try. The type of the exception determines which catch statement is
executed. That is, if the exception type specified by a catch statement matches that of
the exception, then that catch statement is executed (and all others are bypassed).
When an exception is caught, exOb will receive its value.
Here is an important point: If no exception is thrown, then a try block ends normally,
and all of its catch statements are bypassed. Execution resumes with the first
statement following the last catch. Thus, catch statements are executed only if an
exception is thrown.



Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   39   40   41   42   43   44   45   46   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə