RuntimeException is the parent class in the Java programming language of all exceptions that are expected to crash the program when they occur. Unlike Exceptions that are not also RuntimeExceptions (note that RuntimeException extends Exception, so all RuntimeExceptions are also Exceptions), RuntimeExceptions are unchecked. A method that could potentially throw a RuntimeException is not expected to declare that it can throw an exception, nor is the program required to catch a RuntimeException.

A RuntimeException usually reflects a programmer's error, rather than an exceptional condition the program can be expected to deal with. They are also used when a can't happen condition happens. Note that they are not used in situations where the program must not catch the exception; in those cases, such as a program running out of memory, an Error is thrown rather than an Exception. This is useful, because although it is bad programming style, many programs have catch(Exception e){...} statements that would otherwise catch this error that is dangerous to stop. A RuntimeException, conversely, can be caught.

This does not mean that it is usually a good idea to catch a RuntimeException. The program is not expected to deal with one, so unless the exception is expected to be caused by bletcherous input that the program can more cleanly reject, such as the NumberFormatException thrown by the built-in number parsing algorithms, a RuntimeException usually represents a programmer's error. It is better to let these sorts of issues terminate the program so the error can be found.

The three most common RuntimeExceptions are the ArrayIndexOutOfBoundsException (also known as an AIOOBE), the NullPointerException, and the InvalidArgumentException. The first two are thrown by the Java Virtual Machine itself, rather than a throw statement within the program; they occur where C++ would most likely give a segmentation fault.

Log in or register to write something here or to contact authors.