-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
39 lines (34 loc) · 1.17 KB
/
Main.java
File metadata and controls
39 lines (34 loc) · 1.17 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
public class Main {
public static void main(String[] args) {
suppressedExceptionsDemo();
catchMultipleExceptionsDemo();
}
private static void suppressedExceptionsDemo() {
try {
accessDirtyResource();
} catch (Exception e) {
System.out.println("Original exception: " + e.getMessage());
// Let's get the exception which might have occurred after we hit our first exception
final Throwable[] suppressedExceptions = e.getSuppressed();
if (suppressedExceptions.length > 0) {
for (final Throwable suppressedException : suppressedExceptions) {
System.out.println("Suppressed exception: " + suppressedException.getMessage());
}
}
}
}
private static void accessDirtyResource() throws Exception {
try (DirtyResource dirtyResource = new DirtyResource()) {
dirtyResource.readDirtyResource();
}
}
private static void catchMultipleExceptionsDemo() {
int[] array = {1, 2, 3};
try {
System.out.println(array[10]);
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
// No need of multiple catch statements
System.out.println(e.getMessage());
}
}
}