Java Program to Handle Runtime Exceptions Last Updated : 17 Nov, 2020 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 infoAdvertise with us Next Article Java Program to Handle Runtime Exceptions mukulsomukesh Follow Improve Article Tags : Java Java Programs Java-Exception Handling Practice Tags : Java Similar Reads Java Program to Handle the Exception Methods An unlikely event which disrupts the normal flow of the program is known as an Exception. Java Exception Handling is an object-oriented way to handle exceptions. When an error occurs during the execution of the program, an exception object is created which contains the information about the hierarch 4 min read Java Program to Handle Unchecked Exception Exceptions are the issues arising at the runtime resulting in an abrupt flow of working of the program. Remember exceptions are never thrown at the compile-time rather always at runtime be it of any type. No exception is thrown at compile time. Throwable Is super-class of all exceptions and errors t 4 min read Java Program to Handle the Exception Hierarchies Exceptions are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program and terminates the program. Exception Handling: The process of dealing with exceptions is known as Exception Handling. Hierarchy of Exceptions: 4 min read Java Program to Handle Checked Exception Checked exceptions are the subclass of the Exception class. These types of exceptions need to be handled during the compile time of the program. These exceptions can be handled by the try-catch block or by using throws keyword otherwise the program will give a compilation error.  ClassNotFoundExcept 5 min read Java Program to Use Exceptions with Thread Exceptions are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program. When a method encounters an abnormal condition that it can not handle, an exception is thrown as an exception statement. Exceptions are caught 5 min read Java Program to Handle Divide By Zero and Multiple Exceptions Exceptions These are the events that occur due to the programmer error or machine error which causes a disturbance in the normal flow of execution of the program. Handling Multiple exceptions: There are two methods to handle multiple exceptions in java. Using a Single try-catch block try statement a 3 min read How to Handle a java.lang.ArithmeticException in Java? In Java programming, the java.lang.ArithmeticException is an unchecked exception of arithmetic operations. This means you try to divisible by zero, which raises the runtime error. This error can be handled with the ArthmeticException. ArithmeticException can be defined as a runtime exception that ca 2 min read Java Program to Use finally block for Catching Exceptions The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occ 3 min read Using throw, catch and instanceof to handle Exceptions in Java Prerequisite : Try-Catch Block in Java In Java, it is possible that your program may encounter exceptions, for which the language provides try-catch statements to handle them. However, there is a possibility that the piece of code enclosed inside the 'try' block may be vulnerable to more than one ex 4 min read Like