Someone please explain the difference between java.lang.RuntimeException
and java.lang.Exception
? How do I decide which one to extend if I create my own exception?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
In Java, there are two types of exceptions: checked exceptions and un-checked exceptions. A checked exception must be handled explicitly by the code, whereas, an un-checked exception does not need to be explicitly handled.
For checked exceptions, you either have to put a try/catch block around the code that could potentially throw the exception, or add a "throws" clause to the method, to indicate that the method might throw this type of exception (which must be handled in the calling class or above).
Any exception that derives from "Exception" is a checked exception, whereas a class that derives from RuntimeException is un-checked. RuntimeExceptions do not need to be explicitly handled by the calling code.
Before looking at the difference between
java.lang.RuntimeException
andjava.lang.Exception
classes, you must know theException
hierarchy. BothException
andError
classes are derived from classThrowable
(which derives from the classObject
). And the classRuntimeException
is derived from classException
.All the exceptions are derived either from
Exception
orRuntimeException
.All the exceptions which derive from
RuntimeException
are referred to as unchecked exceptions. And all the other exceptions are checked exceptions. A checked exception must be caught somewhere in your code, otherwise, it will not compile. That is why they are called checked exceptions. On the other hand, with unchecked exceptions, the calling method is under no obligation to handle or declare it.Therefore all the exceptions which compiler forces you to handle are directly derived from
java.lang.Exception
and all the other which compiler does not force you to handle are derived fromjava.lang.RuntimeException
.Following are some of the direct known subclasses of RuntimeException.
In simple words, if your client/user can recover from the Exception then make it a Checked Exception, if your client can't do anything to recover from the Exception then make it Unchecked RuntimeException. E.g, a RuntimeException would be a programmatic error, like division by zero, no user can do anything about it but the programmer himself, then it is a RuntimeException.
From oracle documentation:
Runtime exceptions represent problems that are the result of a programming problem and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way.
RuntimeExceptions are like "exceptions by invalid use of an api" examples of runtimeexceptions: IllegalStateException, NegativeArraySizeException, NullpointerException
With the Exceptions you must catch it explicitly because you can still do something to recover. Examples of Exceptions are: IOException, TimeoutException, PrintException...
Proper use of RuntimeException?
From Unchecked Exceptions -- The Controversy:
Note that an unchecked exception is one derived from
RuntimeException
and a checked exception is one derived fromException
.Why throw a
RuntimeException
if a client cannot do anything to recover from the exception? The article explains:User-defined Exception can be Checked Exception or Unchecked Exception, It depends on the class it is extending to.
User-defined Exception can be Custom Checked Exception, if it is extending to Exception class
User-defined Exception can be Custom Unchecked Exception , if it is extending to Run time Exception class.
Define a class and make it a child to Exception or Run time Exception