franta-hg@21: /** franta-hg@21: * RAII v Javě franta-hg@21: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@21: * franta-hg@21: * This program is free software: you can redistribute it and/or modify franta-hg@21: * it under the terms of the GNU General Public License as published by franta-hg@21: * the Free Software Foundation, either version 3 of the License, or franta-hg@21: * (at your option) any later version. franta-hg@21: * franta-hg@21: * This program is distributed in the hope that it will be useful, franta-hg@21: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@21: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@21: * GNU General Public License for more details. franta-hg@21: * franta-hg@21: * You should have received a copy of the GNU General Public License franta-hg@21: * along with this program. If not, see . franta-hg@21: */ franta-hg@21: package cz.frantovo.priklady.raii; franta-hg@21: franta-hg@21: /** franta-hg@21: * franta-hg@21: * @author Ing. František Kučera (frantovo.cz) franta-hg@21: */ franta-hg@21: public class RAIIDemo { franta-hg@21: franta-hg@21: public static void main(String[] args) { franta-hg@21: franta-hg@21: /** franta-hg@21: * Bez výjimek: franta-hg@21: */ franta-hg@21: try { franta-hg@21: try (Zdroj a = new Zdroj("a", false, false, false)) { franta-hg@21: try (Zdroj b = new Zdroj("b", false, false, false)) { franta-hg@21: try (Zdroj c = new Zdroj("c", false, false, false)) { franta-hg@21: a.něcoDělej(); franta-hg@21: b.něcoDělej(); franta-hg@21: c.něcoDělej(); franta-hg@21: } // končí rozsah platnosti proměnné c → volá se c.close() franta-hg@21: } // b.close() franta-hg@21: } // a.close() franta-hg@21: franta-hg@21: } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) { franta-hg@21: System.err.println(e); franta-hg@21: } franta-hg@21: franta-hg@21: System.err.println("--------------------------------"); franta-hg@21: franta-hg@21: /** franta-hg@21: * S výjimkami: franta-hg@21: */ franta-hg@21: try { franta-hg@21: try (Zdroj a = new Zdroj("a", false, false, true)) { franta-hg@21: try (Zdroj b = new Zdroj("b", false, false, true)) { franta-hg@21: try (Zdroj c = new Zdroj("c", false, true, true)) { franta-hg@21: a.něcoDělej(); franta-hg@21: b.něcoDělej(); franta-hg@22: //c.něcoDělej(); franta-hg@21: } franta-hg@21: } franta-hg@21: } franta-hg@21: } catch (ChybaVytváření | ChybaZavírání | ChybaDělání e) { franta-hg@21: System.err.println(e); franta-hg@22: for (Throwable t : e.getSuppressed()) { franta-hg@22: System.err.println("\t" + t + " (potlačená výjimka)"); franta-hg@22: } franta-hg@21: } franta-hg@21: franta-hg@21: System.gc(); // aby se GC vůbec stihl spustit franta-hg@21: franta-hg@21: čekej(); // aby GC stihl zavolat finalize() franta-hg@21: franta-hg@21: } franta-hg@21: franta-hg@21: private static void čekej() { franta-hg@21: try { franta-hg@21: Thread.sleep(100); franta-hg@21: } catch (InterruptedException ex) { franta-hg@21: } franta-hg@21: } franta-hg@21: }