OnJava8-Examples/exceptions/CloseExceptions.java
2015-12-15 11:47:04 -08:00

47 lines
1.1 KiB
Java

// exceptions/CloseExceptions.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
class CloseException extends Exception {}
class Reporter2 implements AutoCloseable {
String name = getClass().getSimpleName();
public Reporter2() {
System.out.println("Creating " + name);
}
public void close() throws CloseException {
System.out.println("Closing " + name);
}
}
class Closer extends Reporter2 {
@Override
public void close() throws CloseException {
super.close();
throw new CloseException();
}
}
public class CloseExceptions {
public static void main(String[] args) {
try(First f = new First();
Closer c = new Closer();
Second s = new Second()) {
System.out.println("In body");
} catch(CloseException e) {
System.out.println("Caught: " + e);
}
}
}
/* Output:
Creating First
Creating Closer
Creating Second
In body
Closing Second
Closing Closer
Closing First
Caught: CloseException
*/