-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExceptionHandling.java
More file actions
70 lines (68 loc) · 2.02 KB
/
ExceptionHandling.java
File metadata and controls
70 lines (68 loc) · 2.02 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
68
69
70
package exceptionHandling;
import java.util.Scanner;
public class ExceptionHandling extends CustomExceptionHandler {
public static void main(String[] args) throws Exception {
int a = 0;
// try block with multiple catch blocks
try {
int b = 7 / a;
System.out.println(b);
} catch (ArithmeticException e) {
if (e instanceof ArithmeticException) {
System.out.println("1. arithmetic exception");
} else {
System.out.println("1. any other exception");
}
} catch (Exception e) {
e.printStackTrace();
}
// nested try blocks.
try {
System.out.println("welcome to the exception program");
try {
int[] ar = new int[2];
System.out.println(ar[2]);
} catch (ArithmeticException e) {
System.out.println("3. ArrayIndexOutOfBound");
}
} catch (Exception ee) {
System.out.println("4. hello exception");
}
// try block with resources
// Java 7 onwards supports declaration of resource inside try
try (Scanner sc = new Scanner(System.in)) {
System.out.println("enter a number");
int number = sc.nextInt();
number = number / 0;
} catch (Exception e) {
System.out.println("5. Exception:" + e);
}
// java 9 onwards supports resource declaration outside of the try();
// Scanner s = new Scanner(System.in);
// try (s) { /* java 9 onwards*/
// try {
// System.out.println("enter a String:");
// String name = s.nextLine();
// System.out.println(name);
// } catch (Exception e) {
// System.out.println("6. Exception: " + e);
// }
CustomExceptionHandler ceh = new CustomExceptionHandler();
try {
ceh.getId();
} catch (Exception e) {
System.out.println("7. Exception: " + e);
}
try {
ceh.getName();
} catch (Exception e) {
System.out.println("8.2. exception: " + e);
} finally {
System.out.println("9. finally blcok getting executed.");
ExceptionHandling eh = new ExceptionHandling();
System.out.println(eh instanceof CustomExceptionHandler);
System.out.println(ceh instanceof ExceptionHandling);
}
// s.close();
}
}