Extending Throwable in Java
- by polygenelubricants
Java lets you create an entirely new subtype of Throwable, e.g:
public class FlyingPig extends Throwable { ... }
Now, very rarely, I may do something like this:
throw new FlyingPig("Oink!");
and of course elsewhere:
try { ... } catch (FlyingPig porky) { ... }
My questions are:
Is this a bad idea? And if so, why?
What could've been done to prevent this subtyping if it is a bad idea?
Since it's not preventable (as far as I know), what catastrophies could result?
If this isn't such a bad idea, why not?
How can you make something useful out of the fact that you can extends Throwable?