Java Reference
In-Depth Information
There is something missing in this statement. The compiler error also tells you that there is an uncaught exception,
which must be caught or declared. You know about catching an exception using a try-catch block. However, you
probably do not understand how to declare an exception. You will learn about declaring an exception in the next section.
The System.in.read() method call may throw a java.io.IOException . The compiler error is telling you to place
this method call in a try-catch block, so you can handle the exception. If you do not catch this exception, you need
to include in the declaration of the readChar() method that it might throw a java.io.IOException . You learned in
the previous sections that the runtime handles all uncaught exceptions. So why can't the Java runtime handle
java.io.IOException in this case? Here comes the concept of checked and unchecked exceptions. You need to learn
about checked and unchecked exception types to fully understand the compiler error.
Three kinds of exceptional conditions may occur in a Java program:
In the first category are exceptions that have a higher potential to occur, and you can handle
them. For example, when you read from a file, it is more likely that an I/O error may occur. It
is better to handle these kinds of exceptions in your program. Classes in the exception class
hierarchy (refer to Figure 9-2 ), which are subclasses of the Exception class, including the
Exception class itself and excluding RuntimeException and all its subclasses, fall into this
category. If a method or constructor may throw an exception of this category, you must take an
appropriate action to handle that exception in your code that calls the method or constructor.
What is that “appropriate action” that you need to take to handle these kinds of exceptions?
You may take one of the following two actions:
try-catch block. One of the
catch blocks must be capable of handling the type of exception that may be thrown.
You can place the code that can throw the exception in a
You can specify in the calling method/constructor declaration that it may throw an
exception. You accomplish this by using a throws clause in the method/constructor
declaration.
In the second category are the exceptions that may occur during the course of the execution
of a Java program, and there is little you can do to handle it. For example, you will receive a
java.lang.OutOfMemeoryError exception when the runtime is out of memory. You cannot
do anything to recover from an out of memory error. It is better for you to let the application
crash, and then look at ways to manage the memory more efficiently in your program. Classes
in the exception class hierarchy (refer to Figure 9-2 ), which are subclasses of the Error class,
and the Error class itself, fall into this category of exception. If a piece of code may throw an
exception of this category, the compiler does not insist on taking an action on your part. If an
exception of this kind is thrown at runtime, the runtime will handle it for you by displaying a
detailed error message and halting the application.
In the third category are exceptions that may occur at runtime, and you may be able to recover
from the exceptional condition if you handle them yourself. There are numerous exceptions
in this category. However, if you feel that it is more likely that an exception of this kind may be
thrown, you should handle it in your code. If you attempt to handle them by using try-catch
blocks, your code tends to get cluttered. Classes in the exception class hierarchy (refer to
Figure 9-2 ), which are subclasses of the RuntimeException class, and the RuntimeException
class itself, fall into this category of exception. If a piece of code may throw an exception of
this category, the compiler does not insist on taking an action on your part. If an exception of
this kind is thrown at runtime, the runtime will handle it for you by displaying a detailed error
message and halting the program.
Exceptions in the first category are known as checked exceptions. The Throwable class also falls under
checked exceptions. The Throwable class, the Exception class, and subclasses of the Exception class, excluding
the RuntimeException class and its subclasses, are called checked exceptions. They are called checked exceptions
because the compiler checks that they are handled in the code.
 
Search WWH ::




Custom Search