<< Chapter < Page | Chapter >> Page > |
When your code throws an exception object of a specific type, that object can be caught by an exception handler designed either to:
In other words, an exception handler can catch exceptions of the class specified by the type of its parameter, or can catch exceptions of any subclassof the class specified by the type of its parameter.
More detailed information on exception handling
As explained earlier, except for Throwable objects of type Error and for Throwable.Exception objects of type RuntimeException , Java programs must either handle or declare all Exception objects that are thrown. Otherwise, the compiler will refuse to compile the program.
In other words, all exceptions other than those specified above are checked by the compiler, and the compiler will refuse to compile the program if the exceptions aren't handled or declared. As a result, exceptions other thanthose specified above are often referred to as checked exceptions.
Catching an exception
Just to make certain that we are using the same terminology, a method catches an exception by providing an exception handler whose parameter type is appropriate for that type of exception object. (I will more or less use the terms catch block and exception handler interchangeably.)
The type of the parameter in the catch block must be the class from which the exception was instantiated, or a superclass of that class that residessomewhere between that class and the Throwable class in the inheritance hierarchy.
Declaring an exception
If the code in a method can throw a checked exception, and the method does not provide an exception handler for the type of exception object thrown, themethod must declare that it can throw that exception. The throws keyword is used in the method declaration to declare that it throws an exception of a particular type.
Any checked exception that can be thrown by a method is part of the method's programming interface (see the read method of the InputStream class, which throws IOException , for example). Users of a method must know about the exceptions that a method can throw in order to be able to handle them.Thus, you must declare the exceptions that the method can throw in the method signature.
Checked exceptions
Checked exceptions are all exception objects instantiated from subclasses of the Exception class other than those of the RuntimeException class.
Exceptions of all Exception subclasses other than RuntimeException are checked by the compiler and will result in compiler errors if they are neither caught nor declared .
You will learn how you can create your own exception classes later. Whether your exception objects become checked or not depends on the class that youextend when you define your exception class.
(If you extend a checked exception class, your new exception type will be a checked exception. Otherwise, it will not be a checked exception.)
Exceptions that can be thrown within the scope of a method
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?