Class RuntimeException in Java

Class RuntimeException in Java

RuntimeException is the superclass of all Exceptions which can be thrown during the normal operation of the java virtual machine. A method is required to declare in its throws clause. Any subclasses of the RuntimeException that might be thrown during the execution of the method but not caught.

In simple words, it is the base class for all exceptions that indicate some kind of programming issue or a bug. They originated as our or client programming using our library improperly. Because the indicate bugs, we virtually never catch a RuntimeException.

It occurs during run time and automatically. In our package, we may throw some of the RuntimeExceptions.

For this exception, we can not directly use it, but it has lots of subclasses which can not be declared in the throws clause of a method definition. (They are declared as they are runtime conditions).

The RuntimeException class is the superclass of the standard run-time exceptions that can be thrown in Java. The appropriate subclass of RuntimeException is thrown in response to a run-time error detected at the virtual machine level. A run-time exception represents a run-time condition that can occur generally in any Java method, so a method is not required to declare that it throws any of the run-time exceptions.

A Java program should try to handle all of the standard run-time exception classes since they represent routine abnormal conditions that should be anticipated and caught to prevent program termination.

See also  All About Class BufferedInputStream in Java

It is one of the popular built-in exception classes in java.

A runtime exception is an important child class of exception.

Popular causes of runtime exception:-

  • The file, the code is trying to open does not exist.
  • Trying to create a new object but no memory is available.
  • The class file is being loaded may be missing.
  • The class file that needs to be loaded is in the wrong format.
  • The string, during conversion, found to be having an invalid character.

The structure of the RuntimeException class is given as :

public class java.lang.RuntimeException extends java.lang.Exception{
public RuntimeException();//constructs an empty RuntimeException object that is an object with no message specified.
public RuntimeException(String message);//constructs a RuntimeException object with the message specified.

Apart from these RuntimeException class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

The specialty of Runtime Exception is that it unchecked in nature. So it is not necessary to check for null on every handle that is passed into a method. (We do not know or we do not want to know if the caller of the method has provided a valid handle). In java, part of the standard runtime checking that (java performs internally) if any call is made to a null handle, java automatically throws a NullPointerException.

So we never need to write an Exception specification saying that a method may throw a NullPointerException.As the compiler does not enforce exception specification for RuntimeException, Runtime Exceptions could propagate to the main() method without being caught.

As a result, printStackTrace() is called for that exception as the program exits. It is only possible not to catch an exception like this for RuntimeExceptions since all other handling is carefully enforced by the compiler.

See also  Class DatagramPacket in Java

Runtime Exception represents either a programming error that we can not catch or one that we, as a programmer should have checked in our code. It means Runtime Exceptions represent a programming error. However, this helps in debugging a different way.

public class DoNoCatch{
    static void foo()
        throw new RuntimeException("generating in foo");
    static void goo()
     public static void main(String []args){

the output of the code:
$java -Xmx128M -Xms16M DoNoCatch
Exception in thread “main” java.lang.RuntimeException: generating in foo
at DoNoCatch.main(


Share and Enjoy !

Leave a Reply

Your email address will not be published. Required fields are marked *