Skip to content

Instantly share code, notes, and snippets.

@aeg
Last active October 25, 2015 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeg/84071e262b63948a747d to your computer and use it in GitHub Desktop.
Save aeg/84071e262b63948a747d to your computer and use it in GitHub Desktop.
AutoCloseable のテスト
public class CloseTest {
public static void main(String... args) {
try (TestResource rs = new TestResource()){
System.out.println("try:start");
throw(new Exception());
} catch (Exception e) {
System.out.println("catch:start");
e.printStackTrace();
System.out.println("catch:end");
} finally {
System.out.println("finally:");
}
}
}
try:start
close was called
catch:start
catch:end
finally:
java.lang.Exception
at closeable.CloseTest.main(CloseTest.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
public class TestResource implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("close was called");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment