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();
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 Exception
s but Error
s, that's the point in having it. The thing is, Error
s 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
.