Java Reference
In-Depth Information
How? If you set a variable to zero, null or an empty string, that may just cause the
program to break later, with much greater mystery.
Of course, some methods in the program know how to communicate with the user or
take other remedial action. By allowing the exception to reach those methods, you
make it possible for the exception to be processed by a competent handler.
507
508
S YNTAX 11.2 Exception Specification
accessSpecifier returnType
methodName(parameterType parameterName,. . .)
throws ExceptionClass, ExceptionClass, . .
.
Example:
public void read(FileReader in)
throws IOException
Purpose:
To indicate the checked exceptions that this method can throw
S ELF C HECK
5.
Suppose a method calls the FileReader constructor and the read
method of the FileReader class, which can throw an
IOException . Which throws specification should you use?
6.
Why is a NullPointerException not a checked exception?
11.4 Catching Exceptions
Every exception should be handled somewhere in your program. If an exception has
no handler, an error message is printed, and your program terminates. That may be
fine for a student program. But you would not want a professionally written program
to die just because some method detected an unexpected error. Therefore, you should
install exception handlers for all exceptions that your program might throw.
Search WWH ::




Custom Search