When should Throwable be used instead of new Exception?

WolfmanDragon picture WolfmanDragon · Jan 31, 2009 · Viewed 98.7k times · Source

Given: Throwable is Exception's superclass.

When I read texts on writing your own 'exceptions', I see examples of Throwable being used in the catch block and other texts show new Exception() being used in the catch block. I have yet to see an explanation of when one should use each.

My question is this, when should Throwable be used and when should new Exception() be used?

Inside the catch or else block using either:

throw throwable;

or

throw new Exception();

Answer

Ray Hidayat picture Ray Hidayat · Jan 31, 2009

Always throw an Exception (never a Throwable). You generally don't catch Throwable either, but you can. Throwable is the superclass to Exception and Error, so you would catch Throwable if you wanted to not only catch Exceptions but Errors, that's the point in having it. The thing is, Errors are generally things which a normal application wouldn't and shouldn't catch, so just use Exception unless you have a specific reason to use Throwable.