Nov 25, 2011

Exceptions and Errors

Java contains an elegant exception-handling mechanism. When a method cannot complete normally, there are three choices. You can return a nonsensical value, never ever return, or throw an exception. The first choice is not usually acceptable, and the second is downright antisocial. Applets want to take particular care that this doesn't happen. What's left? Throwing an exception transfers control non-locally to a block of "rescue" code defined in some currently executing method that called on your method, perhaps indirectly (for example, to a context perhaps several frames up the execution stack, but in the same thread). This rescue code is called a "catch block." The objects that get "thrown and caught" are of class Exception, Error, or any class that implements the interface Throwable. These thrown objects describe the exceptional condition and the context in which it occurs. An Exception indicates that a method can't complete its stated mission because of bad arguments or unavailable resources. An Error is more serious and indicates a condition that is abnormal and unexpected. In the Java API these are heavily subclassed to provide more and less specific "flavors" of exception.


Post a Comment