English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
An exception is a problem that occurs during program execution (runtime error). To understand the purpose, let's look at it in a different way.
Generally, when compiling a program, if the .class file is not created at compile time, it is the executable file in Java, and it is executed every time this.classWhen running the program, it should run successfully to execute every line in the program without any issues. However, in some special cases, the JVM encounters some ambiguous situations while executing the program, i.e., it does not know what to do.
Here are some example scenarios-
If the size of your array is10a line in the code tries to access the element at index11element.
If you try to divide a number by 0 (resulting in infinity, and the JVM cannot understand how to evaluate it).
This is called an exception. Each possible exception is represented by a predefined class, and you can find all exception classes in the java.lang package. You can also define your own exceptions.
Some exceptions are indicated at compile time, known as compile-time exceptions or checked exceptions.
When such exceptions occur, you need to use the try-catch block to handle them, or use the throws keyword to throw them out (defer the handling).
If an exception occurs and is not handled, the program will terminate abruptly, and the code after the line causing the exception will not be executed.
Generally, the size of an array is fixed, and each element is accessed using an index. For example, we have created an array of size7The valid expression for accessing the elements of the array will be a [0] to a [6] (length of1)
An exception will be thrown when using a –ve value or a value greater than or equal to the size of the arrayArrayIndexOutOfBoundsException。
For example, if you execute the following code, it will display the elements of the array and ask you to provide an index to select an element. Since the size of the array is7Therefore, the valid index is from 0 to6。
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 following output, then we have requested the index9of the element because it is an invalid index, thus causingArrayIndexOutOfBoundsExceptionand terminated the execution.
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)
To solve this problem, you need to use a try block-catch block wraps the code responsible for handling the exception.
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]); } System.out.println("Please enter the valid index (0 to 6); } } }
Output Result
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)