# What happens if an exception is not handled in a java program?

Java 8Object Oriented ProgrammingProgramming

An exception is an issue (run time error) occurred during the execution of a program. For understanding purpose let us look at it in a different manner.

Generally, when you compile a program, if it gets compiled without a .class file will be created, this is the executable file in Java, and every time you execute this .class file it is supposed to run successfully executing each line in the program without any issues. But, in some exceptional cases, while executing the program, JVM encounters some ambiguous scenarios where it doesn’t know what to do.

Here are some example scenarios −

• If you have an array of size 10 if a line in your code tries to access the 11th element in this array.
• If you are trying to divide a number with 0 which (results to infinity and JVM doesn’t understand how to valuate it).

Such cases are known as exceptions. Each possible exception is represented by a predefined class you can find all the classes of exception in java.lang package. You can also define your own exception.

Certain exceptions are prompted at compile time and are known as compile time exceptions or, checked exceptions.

When such exceptions occur you need to handle them using try-catch block or, throw them (postpone the handling) using the throws keyword.

## if you don’t handle exceptions

When an exception occurred, if you don’t handle it, the program terminates abruptly and the code past the line that caused the exception will not get executed.

## Example

Generally, an array is of fixed size and each element is accessed using the indices. For example, we have created an array with size 7. Then the valid expressions to access the elements of this array will be a[0] to a[6] (length-1).

Whenever, you used an –ve value or, the value greater than or equal to the size of the array, then the ArrayIndexOutOfBoundsException is thrown.

For Example, if you execute the following code, it displays the elements in the array asks you to give the index to select an element. Since the size of the array is 7, the valid index will be 0 to 6.

## Example

import java.util.Arrays;
import java.util.Scanner;
public class AIOBSample {
public static void main(String args[]){
int[] myArray = {1254, 1458, 5687,1457, 4554, 5445, 7524};
System.out.println("Elements in the array are: ");
System.out.println(Arrays.toString(myArray));
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of the required element: ");
int element = sc.nextInt();
System.out.println("Element in the given index is :: "+myArray[element]);
}
}

But if you observe the below output we have requested the element with the index 9 since it is an invalid index an ArrayIndexOutOfBoundsException raised and the execution terminated.

## Run time exception

Elements in the array are:
[897, 56, 78, 90, 12, 123, 75]
Enter the index of the required element:
7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at AIOBSample.main(AIOBSample.java:12)

## Solution

To resolve this, you need to handle the exception by wrapping the code responsible for it, in a try-catch block.

import java.util.Arrays;
import java.util.Scanner;
public class AIOBSample {
public static void main(String args[]){
int[] myArray = {1254, 1458, 5687,1457, 4554, 5445, 7524};
System.out.println("Elements in the array are: ");
System.out.println(Arrays.toString(myArray));
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the index of the required element: ");
int element = sc.nextInt();
System.out.println("Element in the given index is :: "+myArray[element]);
}catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("Please enter the valid index (0 to 6)");
}
}
}

## Output

Elements in the array are:
[1254, 1458, 5687, 1457, 4554, 5445, 7524]
Enter the index of the required element:
7
Please enter the valid index (0 to 6)
Updated on 02-Jul-2020 14:28:11