forked from akash-coded/C133-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomeMadness.java
54 lines (51 loc) · 2.08 KB
/
SomeMadness.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
54
import java.io.IOException;
class DemoContainer {
void fun() throws ArithmeticException, ArrayIndexOutOfBoundsException, IOException {
int[] arr = { 10 };
System.out.println(arr[4]); // ArrayIndexOutOfBoundsException
}
void caller() throws ArithmeticException, ArrayIndexOutOfBoundsException, IOException {
try {
// try {
// throw new ArithmeticException();
// } catch (ArithmeticException e) {
// throw e;
// }
fun();
System.out.println("End of try block"); // Not printed
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.print("Inside catch of caller(): ");
System.out.println(e);
throw e; // Gets substituted by the throw in finally
} finally {
System.out.print("Inside finally block of caller(): ");
try {
throw new ArithmeticException();
} catch (ArithmeticException e) {
System.out.print("Inside nested catch of caller(): ");
throw e; // This is the exception that is actually rethrown
} finally {
System.out.print("Inside nested finally block of caller(): ");
throw new NullPointerException();
}
}
}
}
public class SomeMadness {
public static void main(String[] args) throws IOException {
DemoContainer c = new DemoContainer();
try {
System.out.print("Inside main(): ");
c.caller();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Fallback logic for array index out of bounds" + e);
} catch (ArithmeticException e) {
System.out.println("Fallback logic for arithmetic exception" + e);
} catch (NullPointerException e) {
System.out.println("Fallback logic for null pointer exception" + e);
} catch (Exception e) {
System.out.println("Fallback logic for exception" + e);
}
System.out.println("End of main");
}
}