Java Reference
In-Depth Information
12.1. Creating Exception Types
Exceptions are objects. All exception typesthat is, any class designed
for throwable objectsmust extend the class THRowable or one of its sub-
classes. The THRowable class contains a string that can be used to describe
the exceptionit is set via a constructor parameter and may be retrieved
with the getMessage method. By convention, most new exception types
extend Exception , a subclass of THRowable . Exception types are not allowed
to be generic types.
Exceptions are primarily checked exceptions, meaning that the compiler
checks that your methods throw only the exceptions they have declared
themselves to throw. The standard runtime exceptions and errors extend
one of the classes RuntimeException and Error , making them unchecked
exceptions. Here is the top of the exception type hierarchy:
Checked exceptions represent conditions that, although exceptional, can
reasonably be expected to occur, and if they do occur must be dealt with
in some way. Making these exceptions checked documents the existence
of the exception and ensures that the caller of a method deals with the
exception in some wayor at least consciously chooses to ignore it.
Unchecked runtime exceptions represent conditions that, generally
speaking, reflect errors in your program's logic and cannot be reasonably
recovered from at run time. For example, the ArrayIndexOutOfBoundsExcep-
tion thrown when you access outside the bounds of an array tells you
 
Search WWH ::




Custom Search