-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCleanupIdiom.java
More file actions
80 lines (69 loc) · 1.76 KB
/
CleanupIdiom.java
File metadata and controls
80 lines (69 loc) · 1.76 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
71
72
73
74
75
76
77
78
79
80
package exceptions;
/**
* RUN:
* javac exceptions/CleanupIdiom.java && java exceptions.CleanupIdiom
* OUTPUT:
* NeedsCleanup 1 completed
* NeedsCleanup 3 completed
* NeedsCleanup 2 completed
* NeedsCleanup 5 completed
* NeedsCleanup 4 completed
*/
// import java.util.logging.*;
// import java.io.*;
public class CleanupIdiom {
public static void main(String[] args)
{
// section 1.
NeedsCleanup nc1 = new NeedsCleanup();
try {
// . . .
}
finally {
nc1.dispose();
}
// section 2.
NeedsCleanup nc2 = new NeedsCleanup();
NeedsCleanup nc3 = new NeedsCleanup();
try {
// . . .
}
finally {
nc3.dispose();
nc2.dispose();
}
// section 3.
try {
NeedsCleanup2 nc4 = new NeedsCleanup2();
try {
NeedsCleanup2 nc5 = new NeedsCleanup2();
try {
// . . .
}
finally {
nc5.dispose();
}
}
catch(ConstructionException e) {
System.out.println(e);
}
finally {
nc4.dispose();
}
}
catch(ConstructionException e) {
System.out.println(e);
}
}
}
class NeedsCleanup {
private static long counter = 1;
private final long id = counter++;
public void dispose() {
System.out.println("NeedsCleanup "+ id + " completed");
}
}
class ConstructionException extends Exception {}
class NeedsCleanup2 extends NeedsCleanup {
public NeedsCleanup2() throws ConstructionException {}
}