Java lets you create an entirely new subtype of Throwable
, e.g:
public class FlyingPig extends Throwable { ... }
Now, very
Is this technique of (ab)using exception valid? (Is there a name for it?)
In my opinion, it's not a good idea:
Exception should just not be used for flow control. If I had to name this technique, I would call it a code smell or an anti pattern.
See also:
If valid, should
FlyingPig
extendsThrowable
, or isException
just fine? (even though there's nothing exceptional about its circumstances?)
There might be situations where you want to catch Throwable
to not only catch Exception
but also Error
but it's rare, people usually don't catch Throwable
. But I fail at finding a situation where you'd like to throw a subclass of Throwable
.
And I also think that extending Throwable
does not make things look less "exception-al", they make it look worse - which totally defeats the intention.
So to conclude, if you really want to throw something, throw a subclass of Exception
.
See also: