-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path10.ExceptionExamples.java
51 lines (50 loc) · 1.34 KB
/
10.ExceptionExamples.java
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
/*Question-
Create a program to take input from user as 1 /2 /3, if
user enters 1, display info about Arithmetic Exception, 2
and 3 for Null pointer exception and Array index out of
bound exception, respectively. Use, Try, Catch block for
each exception individually.*/
//Code-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=null;
int i=0;
while(i<3) {
System.out.println("Enter Value 1/2/3: ");
int n = sc.nextInt();
switch (n) {
//Arithmetic Exception
case 1:
try {
int data = 5 / 0;
}
catch (ArithmeticException e) {
System.out.println(e);
}
System.out.println("rest of the code");
break;
case 2:
//Nullpointer Exception
try {
int m = s.length();
}
catch (NullPointerException f) {
System.out.println(f);
}
System.out.println("rest of code");
break;
case 3:
try {
int arr[] = {1, 3, 5, 7};
System.out.println(arr[10]);
} catch (ArrayIndexOutOfBoundsException g) {
System.out.println(g);
}
System.out.println("rest of the code");
}
i++;
}
}
}