-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tutorial18_TryCatch.java
44 lines (42 loc) · 1.57 KB
/
Tutorial18_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
import jdk.jfr.Experimental;
import java.security.spec.ECField;
import java.util.Scanner;
public class Tutorial18_TryCatch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
System.out.print("Give a: ");
a = sc.nextInt();
System.out.print("Give b: ");
b = sc.nextInt();
try{
System.out.printf("%d when divided by %d gives: %d.\n", a, b, a/b);
}
catch (Exception e){
System.out.println(e);
}
// note we can have try and multiple catch blocks //
int index1, index2;
int [] arr = {11, 21, 34, 16, 15};
try{
System.out.print("Give index1: ");
index1 = sc.nextInt();
System.out.printf("Element at %d index is: %d.\n", index1, arr[index1]);
System.out.print("Give index2: ");
index2 = sc.nextInt();
System.out.printf("Element at %d index is: %d.\n", index2, arr[index2]);
System.out.print("Give b: ");
b = sc.nextInt();
System.out.printf("%d when divided by %d gives: %d.\n", arr[index1]+arr[index2], b, (arr[index1]+arr[index2])/b);
}
catch(ArithmeticException e){
System.out.println("Arithmetic exception!");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds Exception!");
}
catch(Exception e){
System.out.println("Some exception occurred!");
}
}
}