-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryCatchResource.java
More file actions
56 lines (46 loc) · 1.49 KB
/
Copy pathTryCatchResource.java
File metadata and controls
56 lines (46 loc) · 1.49 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
package exception;
public class TryCatchResource {
public static void main(String[] args) throws Exception {
try(MyFileClass myClass = new MyFileClass(1)) {
System.out.println("Try block");
}
try(MyFileClass myClass2 = new MyFileClass(2)) {
System.out.println("Try Block");
}
catch (Exception e) {
e.printStackTrace();
}finally {
System.out.println("Finally Block");
}
System.out.println();
System.out.println("-----------Error While Close in Try-Catch Resource--------------------");
try (MyResource res = new MyResource()) {
res.use();
} catch (Exception e) {
System.out.println("Caught: " + e.getMessage());
for (Throwable t : e.getSuppressed()) {
System.out.println("Suppressed: " + t);
}
}
}
}
class MyFileClass implements AutoCloseable {
private final int num;
public MyFileClass(int num) {
this.num = num;
}
@Override
public void close() throws Exception {
System.out.println("Closing myFileClass #" + num);
}
}
class MyResource implements AutoCloseable {
public void close() throws Exception {
System.out.println("Closing resource");
throw new Exception("Exception in close");
}
public void use() throws Exception {
System.out.println("Using resource");
throw new Exception("Exception in use");
}
}