3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.priklady.raii;
22 * @author Ing. František Kučera (frantovo.cz)
24 public class RAIIDemo {
26 public static void main(String[] args) {
32 try (Zdroj a = new Zdroj("a", false, false, false)) {
33 try (Zdroj b = new Zdroj("b", false, false, false)) {
34 try (Zdroj c = new Zdroj("c", false, false, false)) {
38 } // končí rozsah platnosti proměnné c → volá se c.close()
42 } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) {
46 System.err.println("--------------------------------");
52 try (Zdroj a = new Zdroj("a", false, false, true)) {
53 try (Zdroj b = new Zdroj("b", false, false, true)) {
54 try (Zdroj c = new Zdroj("c", false, true, true)) {
61 } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) {
65 System.gc(); // aby se GC vůbec stihl spustit
67 čekej(); // aby GC stihl zavolat finalize()
71 private static void vypišChybu(final cz.frantovo.priklady.raii.Chyba e) {
72 System.err.println(e);
73 for (Throwable t : e.getSuppressed()) {
74 System.err.println("\t" + t + " (potlačená výjimka)");
78 private static void čekej() {
81 } catch (InterruptedException ex) {