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
There are two types of exception, You can recover from checked exception if you get such kind of exception. Runtime exception are irrecoverable, runtime exceptions are programming errors, and programmer should take care of it while writing the code, and continue execution of this might give you incorrect result. Runtime exceptions are about violating precondition ex. you have an array of size 10, and you are trying to access 11 th element, it will throw ArrayIndexOutOfBoundException
RuntimeException is a child class of Exception class
This is one of the many child classes of Exception class. RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.
The hierchy is
java.lang.Object
---java.lang.Throwable
-------java.lang.Exception
-------------java.lang.RuntimeException
Exceptions are a good way to handle unexpected events in your application flow. RuntimeException are unchecked by the Compiler but you may prefer to use Exceptions that extend Exception Class to control the behaviour of your api clients as they are required to catch errors for them to compile. Also forms good documentation.
If want to achieve clean interface use inheritance to subclass the different types of exception your application has and then expose the parent exception.
An Exception is checked, and a RuntimeException is unchecked.
Checked means that the compiler requires that you handle the exception in a catch, or declare your method as throwing it (or one of its superclasses).
Generally, throw a checked exception if the caller of the API is expected to handle the exception, and an unchecked exception if it is something the caller would not normally be able to handle, such as an error with one of the parameters, i.e. a programming mistake.
The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking, since the compiler cannot establish that run-time exceptions cannot occur. (from JLS).
In the classes that you design you should subclass Exception and throw instances of it to signal any exceptional scenarios. Doing so you will be explicitly signaling the clients of your class that usage of your class might throw exception and they have to take steps to handle those exceptional scenarios.
Below code snippets explain this point:
In the above class definition of class Process, the method
execute
can throw a RuntimeException but the method declaration need not specify that it throws RuntimeException.The method
process
throws a checked exception and it should declare that it will throw a checked exception of kind MyException and not doing so will be a compile error.The above class definition will affect the code that uses Process class as well.
The call
new Process().execute()
is a valid invocation where as the call of formnew Process().process()
gives a compile error. This is because the client code should take steps to handleMyException
(say call to process() can be enclosed in a try/catch block).Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g
NullPointerException
,ArrayIndexOutOfBoundException
. If you check fornull
before calling any method,NullPointerException
would never occur. SimilarlyArrayIndexOutOfBoundException
would never occur if you check the index first.RuntimeException
are not checked by the compiler, so it is clean code.EDIT : These days people favor
RuntimeException
because the clean code it produces. It is totally a personal choice.