Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling RuntimeExceptions in Java [closed]

Tags:

Can anyone explain how to handle the Runtime Exceptions in Java?

like image 391
RKCY Avatar asked Jan 08 '10 15:01

RKCY


People also ask

How to handle 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.

What is RuntimeException in Java?

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.

What is the difference between runtime exceptions and runtime errors?

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.

What if a client cannot recover from a RuntimeException?

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:


1 Answers

It doesn't differ from handling a regular exception:

try {
   someMethodThatThrowsRuntimeException();
} catch (RuntimeException ex) {
   // do something with the runtime exception
}
like image 85
Bozho Avatar answered Oct 11 '22 02:10

Bozho