In: Computer Science
An exception with finally block lab.
Solution
code
import java.util.*;
class ReadArray {
public static void main(String[] args) {
try {
Scanner sc=new Scanner(System.in);
int[] array=new int[5];
System.out.println("Enter 5 numbers");
for(int i=0;i<6;i++)
array[i]=sc.nextInt();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutofBoundsException=> " +
e.getMessage());
} finally {
System.out.println("Operation Completed");
}
}
}
Screenshot
Output
Explanation
Array size we declared as 5
if we entered 5 numbers the program will not find any exceptio
if you are trying to read the 6th number it will catch
Arrayindexoutof bound exception