Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Exception class - extends from Exception or Thowable? [duplicate]

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.

like image 697
Jay Avatar asked Feb 15 '13 09:02

Jay


People also ask

Can I create custom exception by extending throwable class?

No, it is nor mandatory to extend the exception class to create custom exceptions, you can create them just by extending Throwable class, the super class of all exceptions.

Why should we extend exception instead of throwable?

In what situations we extend Throwable. Never extends throwable, extends Exception or an existing subclass of Exception as you want to add another kind of Exception not a kind of Throwable. You can extends RuntimeException if you need your exception to be unchecked. Because "Throwable" is too non-specific.

Which of the following is correct all classes of exception extend from throwable?

d. All classes of Exception extends Throwable. Both Exception and Error extend Throwable , so (a) and (b) are false and (d) is true.

Do all exceptions in Java extend the throwable class?

All objects within the Java exception class hierarchy extend from the Throwable superclass. Only instances of Throwable (or an inherited subclass) are indirectly thrown by the Java Virtual Machine (JVM), or can be directly thrown via a throw statement.


2 Answers

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.

like image 142
Ostap Andrusiv Avatar answered Sep 28 '22 09:09

Ostap Andrusiv


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.

like image 25
RAS Avatar answered Sep 28 '22 09:09

RAS