Extending Throwable in Java

Posted by polygenelubricants on Stack Overflow See other posts from Stack Overflow or by polygenelubricants
Published on 2010-05-03T03:29:24Z Indexed on 2010/05/03 3:38 UTC
Read the original article Hit count: 333

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about throwable