-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriver.java
52 lines (41 loc) · 1.46 KB
/
Driver.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
import java.io.IOException;
/**
* This code was written to demonstrate that exceptions thrown in the constructor of an
* {@link AutoCloseable} can interrupt the safety that a try-with-resources block would suggest, so
* extra care should be taken in those constructors.
*/
public class Driver {
public static void main(String[] args) {
try (EvilResource resource = new EvilResource()) {
resource.doThings();
} catch (IOException e) {
System.out.println("Catch: " + e.getMessage());
}
if (!EvilResource.closeWasCalled) {
System.out.println("Not closed :(");
} else {
System.out.println("Closed :)");
}
}
/**
* An "Evil" implementation of {@link AutoCloseable} that will intentionally throw exceptions for
* demonstration purposes.
*/
public static class EvilResource implements AutoCloseable {
public static boolean closeWasCalled = false;
public EvilResource() throws IOException {
System.out.println("I am constructing...");
// consider commenting out this line and running the program with or without it
throw new IOException("Uh oh! (constructor)");
}
public void doThings() throws IOException {
System.out.println("I am reading the resources...");
// exceptions here are "OK" in the sense that the try-with-resources will complete normally
throw new IOException("Uh oh! (doThings())");
}
@Override
public void close() throws IOException {
EvilResource.closeWasCalled = true;
}
}
}