Handle Runtime Exception in Java



The error that occurs at runtime after successful compilation of the Java program is called a runtime error or unchecked exception. It disrupts the normal flow of a program's execution and terminates the program abruptly.

These errors are not detected by the compiler but by JVM. The runtime errors in Java are represented by a class called RuntimeException.

In this article, we are going to learn what RuntimeException is and its common types. Also, we will discuss how to handle RuntimeException in Java.

What is RuntimeException in Java?

In Java, the RuntimeException of the java.lang package is a parent class of all exceptions that are expected to crash or break down the program or application when they occur. It helps developers catch errors at runtime.

Unlike checked exceptions that are checked during compilation, the RuntimeExceptions are never checked and do not generate a compile-time error.

The RuntimeException is also used when a condition that can't happen. It should be noted that when a program is running out of memory, a program error is thrown instead of showing it as a RuntimeException.

Examples of RuntimeException in Java

The most common RuntimeExceptions are given below:

  • NullPointerException: It is the exception thrown by the Java Virtual Machine when a user performs some operations on a certain object considered as null or is calling for a method on the null object.

  • ArrayIndexOutOfBoundsException: It is the exception that is automatically thrown by the Java Runtime Environment when a certain Java program incorrectly tries to access a certain location in a non-existent collection of data. This often happens when the requested index of an array is negative, or more than or equal to its size.

  • InvalidArgumentException: This is an exception raised when an invalid parameter is passed to a certain method on the server's referenced connection.

All the exceptions discussed above are itself a subclass of RuntimeException.

Example of RuntimeException

In this example, we will see a simple Java program that demonstrates RuntimeException:

public class Example {
   public static void main(String[] args) {
      String text = null;
	  // Throws NullPointerException
      System.out.println(text.length());  
   }
}

When you run the above code, it will give the NullPointerException:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "" is null
at Example.main(Example.java:5)

Handling Runtime Exception in Java

RuntimeException in Java can be handled using:

  • if-else conditions
  • try-catch blocks

Using if-else Conditions

The if-else condition prevents the exception by checking if an object is null before accessing it.

Example

Here, we will handle the previous NullPointerException using the if-else condition:

public class Example {
   public static void main(String[] args) {
      String text = null;
      
      if (text != null) {
         System.out.println(text.length());
      } else {
         System.out.println("String is null, cannot determine length");
      }
   }
}

On running the above code, you will get the below result:

String is null, cannot determine length

Using try-catch Blocks

You can also use the try-catch block to handle a RuntimeException. It will catch the exception and handle it without disturbing the flow of the program.

Example

In this Java program, we will demonstrate how to handle ArrayIndexOutOfBoundsException using a try-catch block:

public class Example {
   public static void main(String[] args) {
      int[] numbers = {73, 64, 12};

      try {
         // accessing an invalid index
         System.out.println(numbers[3]); 
      } catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("Invalid index!");
      }
   }
}

Output of the above code is as follows:

Invalid index!
Updated on: 2025-05-14T14:21:29+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements