Java Reference
In-Depth Information
Notice how the ArithmeticException was created using new in the throw statement. Re-
member, throw throws an object. Thus, you must create an object for it to throw. That is,
you can't just throw a type.
Rethrowing an Exception
An exception caught by one catch statement can be rethrown so that it can be caught by an
outer catch . The most likely reason for rethrowing this way is to allow multiple handlers
access to the exception. For example, perhaps one exception handler manages one aspect
of an exception, and a second handler copes with another aspect. Remember, when you
rethrow an exception, it will not be recaught by the same catch statement. It will propagate
to the next catch statement. The following program illustrates rethrowing an exception:
Ask the Expert
Q :
Why would I want to manually throw an exception?
A : Most often, the exceptions that you will throw will be instances of exception classes
that you created. As you will see later in this chapter, creating your own exception
classes allows you to handle errors in your code as part of your program's overall ex-
ception handling strategy.
Search WWH ::




Custom Search