-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_index_error_handling.java
More file actions
35 lines (34 loc) · 1.1 KB
/
Array_index_error_handling.java
File metadata and controls
35 lines (34 loc) · 1.1 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
import java.util.Scanner;
public class ArrayS {
int arr[];
int n;
int index;
ArrayS() {
try {
Scanner s = new Scanner(System.in);
n = s.nextInt();
arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(s.next());
}
index = s.nextInt();
}
catch (NumberFormatException ex1){
System.out.println("NumberFormatException occurred");
}
}
public void error()throws ArrayIndexOutOfBoundsException {
if (index > n) {
throw new ArrayIndexOutOfBoundsException("ArrayIndexOutOfBoundsException occurred");
}
else{
System.out.println(arr[index]);
}
}
void errorHandling(){
try{
error();
}catch (ArrayIndexOutOfBoundsException ex2){
System.out.println("ArrayIndexOutOfBoundsException occurred");
}
}