Spread the love

Class RuntimeException in Java

Spread the love

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.

READ  How to stop a Test Process If that Is Hanged or Went To Infinite loop
READ  Class DatagramPacket in Java

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

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.

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:
$javac DoNoCatch.java
$java -Xmx128M -Xms16M DoNoCatch
Exception in thread “main” java.lang.RuntimeException: generating in foo
at DoNoCatch.foo(DoNoCatch.java:4)
at DoNoCatch.goo(DoNoCatch.java:8)
at DoNoCatch.main(DoNoCatch.java:11)

READ  Class StringSelection in Java
READ  Class InputStream in Java

Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles



Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...


Spread the love