-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_handle.java
More file actions
67 lines (51 loc) · 1.99 KB
/
exception_handle.java
File metadata and controls
67 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index. Index starts from zero.
// This program may generate Array Index Out Of Bounds Exception or NumberFormatException . Use exception handling mechanisms to handle this exception.
// Sample Input and Output 1:
// Enter the number of elements in the array
// 2
// Enter the elements in the array
// 50
// 80
// Enter the index of the array element you want to access
// 1
// The array element at index 1 = 80
// The array element successfully accessed
// Sample Input and Output 2:
// Enter the number of elements in the array
// 2
// Enter the elements in the array
// 50
// 80
// Enter the index of the array element you want to access
// 9
// java.lang.ArrayIndexOutOfBoundsException
// Sample Input and Output 3:
// Enter the number of elements in the array
// 2
// Enter the elements in the array
// 30
// j
// java.lang.NumberFormatException
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionalHANdling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the no of elements in the array: ");
int n = sc.nextInt();
int [] arr = new int[n];
System.out.println("Enter the elements in the array: ");
try{
for(int i = 0; i<n;i++)
arr[i] = sc.nextInt();
System.out.println("Enter the index of array element you wanna access ");
int index = sc.nextInt();
System.out.println("The array element at index" + index + " = "+ arr[index]);
System.out.println("The array element is successfully accessed");
}
catch(ArrayIndexOutOfBoundsException | InputMismatchException | NumberFormatException e){
System.out.println(e.getClass());
}
sc.close();
}
}