Can anyone explain how to handle the Runtime Exceptions in Java?
How to handle the Runtime Exception in Java? The Runtime Exception is the parent class in all exceptions of the Java programming language that are expected to crash or break down the program or application when they occur. Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked.
RuntimeException is the superclass of all classes that exceptions are thrown during the normal operation of the Java VM (Virtual Machine). The RuntimeException and its subclasses are unchecked exceptions. The most common exceptions are NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, InvalidArgumentException etc.
Unlike exceptions that are not considered as Runtime Exceptions, Runtime Exceptions are never checked. The Runtime Exception usually shows the programmer's error, rather than the condition a program is expected to deal with.
If a client cannot do anything to recover from the exception, make it an unchecked exception. Note that an unchecked exception is one derived from RuntimeException and a checked exception is one derived from Exception. Why throw a RuntimeException if a client cannot do anything to recover from the exception? The article explains:
It doesn't differ from handling a regular exception:
try {
someMethodThatThrowsRuntimeException();
} catch (RuntimeException ex) {
// do something with the runtime exception
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With