|
| 1 | +package exceptionsAndAssertions; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | + |
| 9 | +/** |
| 10 | + * |
| 11 | + * @author chengfeili |
| 12 | + * Jun 11, 2017 11:21:42 AM |
| 13 | + * |
| 14 | + * Remember that only a try-with-resources statement is permitted to |
| 15 | + * omit both the catch and finally blocks. A traditional try statement |
| 16 | + * must have either or both. |
| 17 | + */ |
| 18 | +public class TryWithResources { |
| 19 | + /** |
| 20 | + * |
| 21 | + * The new try-with-resources statement automatically closes all |
| 22 | + * resources opened in the try clause. This feature is also |
| 23 | + * known as automatic resource management, because Java |
| 24 | + * automatically takes care of the closing. |
| 25 | + * |
| 26 | + * The resources created in the try clause are only in scope within the try block. |
| 27 | + */ |
| 28 | + public void newApproach(Path path1, Path path2) throws IOException { |
| 29 | + try (BufferedReader in = Files.newBufferedReader(path1); |
| 30 | + BufferedWriter out = Files.newBufferedWriter(path2)) { |
| 31 | + out.write(in.readLine()); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * AutoCloseable |
| 38 | + * |
| 39 | + * You can’t just put any random class in a try-with-resources statement. Java |
| 40 | + * commits to closing automatically any resources opened in the try clause. |
| 41 | + * |
| 42 | + */ |
| 43 | +class TurkeyCage implements AutoCloseable { |
| 44 | + public void close() { |
| 45 | + System.out.println("Close gate"); |
| 46 | + } |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + try (TurkeyCage t = new TurkeyCage()) { |
| 50 | + System.out.println("put turkeys in"); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * |
| 57 | + * What happens if the try block also throws an exception? Java 7 added a way to |
| 58 | + * accumulate exceptions. When multiple exceptions are thrown, all but the first |
| 59 | + * are called suppressed exceptions. |
| 60 | + * |
| 61 | + */ |
| 62 | +class JammedTurkeyCage implements AutoCloseable { |
| 63 | + public void close() throws IllegalStateException { |
| 64 | + throw new IllegalStateException("Cage door does not close"); |
| 65 | + } |
| 66 | + // print |
| 67 | + // caught: turkeys ran off |
| 68 | + // Cage door does not close |
| 69 | + public static void main(String[] args) { |
| 70 | + try (JammedTurkeyCage t = new JammedTurkeyCage()) { |
| 71 | + throw new IllegalStateException("turkeys ran off"); |
| 72 | + } catch (IllegalStateException e) { |
| 73 | + System.out.println("caught: " + e.getMessage()); |
| 74 | + for (Throwable t : e.getSuppressed()) |
| 75 | + System.out.println(t.getMessage()); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * |
| 82 | + * Resources are closed after the try clause ends and before any catch/finally clauses. |
| 83 | + * Resources are closed in the reverse order from which they were |
| 84 | + * created. |
| 85 | + * |
| 86 | + * Print: |
| 87 | + * Close: 2 |
| 88 | + * Close: 1 |
| 89 | + * ex |
| 90 | + * finally |
| 91 | + */ |
| 92 | +class Auto implements AutoCloseable { |
| 93 | + int num; |
| 94 | + |
| 95 | + Auto(int num) { |
| 96 | + this.num = num; |
| 97 | + } |
| 98 | + |
| 99 | + public void close() { |
| 100 | + System.out.println("Close: " + num); |
| 101 | + } |
| 102 | + |
| 103 | + public static void main(String[] args) { |
| 104 | + try (Auto a1 = new Auto(1); Auto a2 = new Auto(2)) { |
| 105 | + throw new RuntimeException(); |
| 106 | + } catch (Exception e) { |
| 107 | + System.out.println("ex"); |
| 108 | + } finally { |
| 109 | + System.out.println("finally"); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments