Extending Throwable
Hi,
Is there any circumstance when extending the Throwable class is justified? When I was fresh to Exceptions my friend told me that Throwable should never be extended, it is audacious to do that when Throwable's small brother Exception is there.
I now feel the reason for not extending Throwable to create an exception is to avoid casting the net too wide. As it would catch everything under the sun and catching Errors like OutOfMemoryError makes little sense. And whatever the effect, the same can be acheived by catching Throwable.
Please let me know if there is any situtation when extending Throwable is correct or necessary.
Thanks a lot.
[677 byte] By [
janvara] at [2007-10-2 20:35:01]

I've never run across a situation where there was a reason to do so. It's probably not necessarily a bad thing in and of itself, but since it's non-standard, it could cause confusion or make your code harder to understand. If you think you have a good reason why you'd want to do it that would justify that, then do so, and document your reason. Otherwise, just stick to extending Exception, or possibly RuntimeException.
jverda at 2007-7-13 23:18:12 >

Thank you for the reply.
> It's probably not necessarily a bad
> thing in and of itself
Why not? I felt it was a bad thing to do. Whatever that can be acheived by catching an Exception that extends Throwable can be acheived by cathing Throwable itself and also by catching Throwable or its extended class wont we be catching the Errors too..? I mean creating specific Exceptions are useful but not specific Throwables. your thoughts?
Thank you.
The standard API gives you two built-in types of Throwables: Exceptions and Errors.Can you think of something that is throwable but is not an exception nor an error?If you can, by all means derive the class directly from Throwable.
> Thank you for the reply.
>
> > It's probably not necessarily a bad
> > thing in and of itself
>
> Why not? I felt it was a bad thing to do.
By itself, extending Throwable won't do any harm. However, when you take into account that there's no benefit (that I'm aware of) to doing so, and the fact that it's non-standard, then on the whole it seems like a bad idea.
jverda at 2007-7-13 23:18:12 >

I now feel the reason for not extending Throwable to create an exception is to avoid casting the net too wide. As it would catch everything under the sun and catching Errors like OutOfMemoryError makes little sense. And whatever the effect, the same can be acheived by catching Throwable.
You're conflating more than one thing here.
Extending Throwable is probably a bad idea for the reasons already laid down - there are no obvious categories of exception that are not already covered by the Exception, RuntimeException and Error classes.
Catching Throwable is also (generally) a bad idea, because as you say - there are some exceptions that it's probably not worth trying to handle. In point of fact you might want to try to catch Throwable in some limited circumstances (for example where you want to try to log the failure before ceding control).
Using a Throwable reference is often a good idea. For example the logging APIs provide parameters as Throwable types so that the same methods can be used to log Exceptions, RuntimeExceptions, and Errrors.
D.