-
Notifications
You must be signed in to change notification settings - Fork 0
/
userInput_tryCatch.java
53 lines (52 loc) · 1.54 KB
/
userInput_tryCatch.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
52
53
package exception_handling;
import java.util.*;
public class userInput_tryCatch {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("*************** Hello 18BCI0174 - Aryan ****************");
Scanner sc=new Scanner(System.in);
int n;
System.out.println("[*] Enter the choice: \n ~ 1 Arithmetic Exception\n ~ 2 Null Pointer Exception\n ~ 3 Array index out of bound exception");
n=sc.nextInt();
switch(n) {
//if(n==1) {
case 1:{
System.out.println("[-] Lets see what happen on divion of 10 by 0 ->");
try {
int data=10/0;
}
catch(ArithmeticException ae) {
System.out.println("[+] And we have an "+ae+", resulted from reaching an undefined value.");
}
break;}
//}
//else if(n==2) {
case 2:{
System.out.println("[-] Lets see what happen on using a string null value ->");
try {
String data=null;
System.out.println(data.length());
}
catch(NullPointerException npe) {
System.out.println("[+] And we have an "+npe+", resulted from refering null string value.");
}
break;}
//}
//else if(n==3) {
case 3:{
System.out.println("[-] Lets see what happen on referring index 10 for array of length 5 ->");
try {
int[] array= {1,2,3,4,5};
int l=array[10];
}
catch(ArrayIndexOutOfBoundsException aoe) {
System.out.println("[+] And we have an "+aoe+", resulted from referring an imaginary index.");
}
break;}
//}
default:{
System.out.println("[!] Good bye!");
}
}
}
}