Java distinguishes between checked
exceptions and unchecked exceptions.
This distinction is important, because the Java compiler enforces a catch-or-declare requirement for checked exceptions. An exception’s
type determines whether it’s checked or unchecked. All exception types that are
direct or indirect subclasses of class RuntimeException (package java.lang) are unchecked
exceptions. These are typically caused by defects in your program’s code.
Examples of unchecked exceptions include ArrayIndexOutOfBoundsExceptions (discussed in Chapter 7) and ArithmeticExceptions (shown in Fig. 11.3). All classes that
inherit from class Exception but not class RuntimeException are considered to be checked
exceptions. Such exceptions are typically caused by conditions that are not
under the control of the program — for example, in file processing, the program
can’t open a file because the file does not exist. Classes that inherit from
class Error are considered to be unchecked.
The compiler checks each method call and method declaration to
determine whether
the method throws checked exceptions. If so, the compiler verifies
that the checked exception is caught or is declared in a throws clause. We show
how to catch and declare checked exceptions in the next several examples.
Recall from Section 11.3 that the throws clause specifies the exceptions a
method throws. Such exceptions are not caught in the method’s body. To satisfy
the catch part of the catch-or-declare requirement, the code that generates the
exception must be wrapped in a try block and must provide a catch handler for
the checked-exception type (or one of its superclass types). To satisfy the
declare part of the catch-or-declare requirement, the method containing the
code that generates the exception must provide a throws clause containing the
checked-exception type after its parameter list and before its method body. If
the catch-or-declare requirement is not satisfied, the compiler will issue an
error message indicating that the exception must be caught or declared. This
forces you to think about the problems that may occur when a method that throws
checked exceptions is called.
Unlike checked exceptions, the Java compiler does not check the
code to determine
whether an unchecked exception is caught or declared. Unchecked
exceptions typically can be prevented by proper coding. For example, the
unchecked ArithmeticException exception can be avoided if the method ensures
that the denominator is not zero before attempting to perform the division.
Unchecked exceptions are not required to be listed in a method’s throws
clause—even if they are, it’s not required that such exceptions be caught by an
application.
This is a very nice post I m very like it and I appreciate you for good work keep it up it is very useful for me.
ReplyDeletejavascript training in chennai
javascript training
javascript training courses
javascript training classes
core java training in chennai
C C++ Training in Chennai
Appium Training in Chennai
JMeter Training in Chennai