Java Program to Handle Runtime Exceptions Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. The NullPointerException is the exception thrown by the JVM when the program tries to call a method on the null object or perform other operations on a null object. A user should not attempt to handle this kind of exception because it will only patch the problem and not completely fix it.The ArrayIndexOutOfBoundsException is the exception that is automatically thrown by the JRE(Java Runtime Environment) when a program incorrectly tries to access a certain location in a set that is non-existent. This often happens when the array index requested is negative, or more than or equal to the array's size. The arrays of Java use the zero-based indexing; thus, the first element of that array has a zero index, the last element comes with an index of size 1, and the nth element comes with an index n-1.The InvalidArgumentException is an exception raised when an invalid parameter is passed to a certain method on the server's referenced connection. Example 1: Java // Create public class public class GFG { public void GreeksForGreeks() { // throw exception throw new Greeks(); } public static void main(String[] args) { try { new GFG().GreeksForGreeks(); } // catch exception catch (Exception x) { System.out.println( "example of runtime exception"); } } } // create subclass and extend RuntimeException class class Greeks extends RuntimeException { // create constructor of this class public Greeks() { super(); } } Outputexample of runtime exception Now let's create one more common example of run time exception called ArrayIndexOutOfBoundsException. This exception occurs when we want to access array more than its size for example we have an array of size 5, array[5] and we want to access array[6]. This is an ArrayIndexOutOfBoundsException because 6 indexes does not exist in this array. Example 2: Java class GFG { public static void main(String[] args) { // create array of 5 size int[] a = new int[] { 1, 2, 3, 4, 5 }; // execute for loop for (int i = 0; i < 6; i++) { // print the value of array System.out.println(a[i]); } } } Output Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at GFG.main(File.java:10) Comment More info M mukulsomukesh Follow Improve Article Tags : Java Java Programs Java-Exception Handling Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java5 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java5 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like