Java Programming/Checked Exceptions
From Wikibooks, the open-content textbooks collection
| Navigate Exceptions topic: |
Contents |
[edit] Introduction
Checked or caught exceptions are those exceptions that the application programmer created a catch block to handle them. The java compiler gives a hand here by making sure that all exceptions that is sub-classed from the java.lang.Exception class, but not sub-classed from the java.lang.RuntimeException, must be caught by the application programmer. Those exceptions are called the application or business logic exceptions.
So the compiler for the Java programming language checks, at compile time, that a program contains handlers for all application exceptions, by analyzing each method body. If, by executing the method body, an exception can be thrown to the caller, that exception must be declared by the throws keyword. How does the compiler know whether a method body can throw an exception? That is easy. Inside the method body, there are calls to other methods; the compiler looks at each of their method signature, what exceptions they declared to throw. There are two possibilities, either exceptions are caught inside the caller-method by a try-catch block, or the caller declares to throw the exception to its caller.
It is possible that a method declares that it can throw an exception, but actually it does not. For example, the below method declares to throw an exception, even if it currently does not. Still, the caller has to deal with it.
public String calculateBusiness() throws NoBusinessExeption { }
Even if the method now does not throw the exception, it may do in the future.
When I call the above method, I can decide to handle the NoBusinessException, or I can pass it over to my caller.
public String myBusiness() throws NoBusinessExeption { ... calculateBusiness(); ... }
public String myBusiness() { try { calculateBusiness(); } catch ( NoBusinessExeption e ) { // --- handle it } ... }
[edit] Why force exception handling ?
This compile-time checking for the presence of exception handlers is designed to make the application programmer life easier. To debug whether a particular thrown exception has a matching catch would be a long process. In conventional languages like C, and C++, a separate error handling debugging were needed. In java we can be sure that when an application exception is thrown, that exception somewhere in the program is handled. In C, and C++, that has to be tested. In Java that does not need to be tested, so the freed up time can be used for more meaningful testing, testing the business functionalities.
[edit] What exceptions can be declared, when overriding a method
The checked exception classes specified after the throws keyword are part of the contract between the implementor and user. An overriding method may not specify that this new method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw.
[edit] What exceptions can be declared, when implementing an Interface
When interfaces are involved, the implementation declaration must have a throws-clause that is compatible with the interface declarations.