19

I am designing a custom Exception class for my application. I have a very basic question. Should I extend from Exception class or Thowable class ? What are the benefits ?

I intend to throw this from underlying layers and catch it in the top level classes. Will it influence my decision of using Thowable over Exception. Is it fundamentally right to catch a Thowable ?

I 've gone through some other threads in this forum. They talk about having the stack trace maintained when it is thrown and not having it for exception etc. I understand that some say ( here) that Thowable is super class of Exception and we should not use it. But others (here) say Exception is for "Exceptional " cases.

This question is rather a discussion of how one is better than other rather than asking how.

Community
  • 1
  • 1
Jay
  • 690
  • 1
  • 10
  • 27
  • 2
    In the end, what you are going to catch at the top level is `YourException` - whether it is an Exception or a Throwable does not make much difference. If others might use your API and have to catch that Exception, extending Exception would probably create less surprise. – assylias Feb 15 '13 at 09:40
  • Thank you for the reply. Yea, I agree with it. I was just making sure my decision is right. I don't want any pne picking on me. :) – Jay Feb 15 '13 at 09:46

3 Answers3

30

Throwable is a class for all the bad situations, which can arise: Errors & Exceptions.

Error is something, you can't handle at all: OutOfMemoryError, VirtualMachineError, etc.

Exception is for exceptional cases.

Exceptions come in 2 flavours:

  1. RuntimeExceptions.

    These ones, you are not aware of: NullPointerException, ClassCastException, etc.

  2. Checked exceptions.

    These are the exceptions, which your code is aware of and should be explicitely catched (... throws MyException): IOExceptions, etc.

If you want the users of your code, to explicitely handle some exceptional situations, it would be good to just extend Exception, not the RuntimeException. There's no need to extend Throwable.

Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38
4

Fundamentally you should extends Exception class as you are creating Custom Exception. Exception and Error both extends Throwable, It really does not make sense extending Throwable.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
4

Throwable is the super class of Error & Exception.

Like Exception, Error too, can be thrown & handled.

But it is not advisable, according to the following doc:

You are not required to catch Error objects or Error subtypes. You can also throw an Error yourself (although other than AssertionError you probably won't ever want to), and you can catch one, but again, you probably won't. What, for example, would you actually do if you got an OutOfMemoryError?

Keeping this concept in mind, I would suggest to extend Throwable if you want to throw and/or catch Exception & Error both. Extend Exception if you want to throw and/or catch Exception only.

RAS
  • 8,100
  • 16
  • 64
  • 86