Closeable 和 AutoCloseable close() 方法的执行顺序
Order of execution of Closeable and AutoCloseable close() methods
有人可以向我解释这里发生了什么,顺序是什么?。输出对我来说没有任何意义。
输出为T 1 IOE F。
密码是:
import java.io.Closeable;
import java.io.IOException;
public class TestRes {
public static void main(String[] args) {
try (
MyResource11 r1 = new MyResource11();
MyResource21 r2 = new MyResource21();
)
{
System.out.print("T ");
} catch (IOException ioe) {
System.out.print("IOE ");
} finally {
System.out.print("F ");
}
}
}
class MyResource11 implements AutoCloseable {
public void close() throws IOException {
System.out.print("1 ");
}
}
class MyResource21 implements Closeable {
public void close() throws IOException {
throw new IOException();
}
}
try-with-resources 以 相反 的顺序关闭资源,与它们声明的顺序相反。所以代码:
- 打印
T
- try-with-resources 语句试图关闭
r2
- 抛出异常
- try-with-resources 语句成功关闭
r1
,输出 1
- 异常块是运行(来自
r2
的异常)并输出IOE
- finally块是运行,输出
F
try-with-resources part of the JLS 值得一读,其中包含未解开的 try-with-resources 语句的代码示例(例如,仅包含 try
/catch
/[ 的等效代码=19=]).来自该部分:
Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.
您的代码是
的快捷方式
public static void main(String[] args) {
try {
MyResource11 r1 = new MyResource11();
try {
MyResource21 r2 = new MyResource21();
try {
System.out.print("T ");
} finally {
r2.close();
}
} finally {
r1.close();
}
} catch (IOException ioe) {
System.out.print("IOE ");
} finally {
System.out.print("F ");
}
}
有人可以向我解释这里发生了什么,顺序是什么?。输出对我来说没有任何意义。
输出为T 1 IOE F。
密码是:
import java.io.Closeable;
import java.io.IOException;
public class TestRes {
public static void main(String[] args) {
try (
MyResource11 r1 = new MyResource11();
MyResource21 r2 = new MyResource21();
)
{
System.out.print("T ");
} catch (IOException ioe) {
System.out.print("IOE ");
} finally {
System.out.print("F ");
}
}
}
class MyResource11 implements AutoCloseable {
public void close() throws IOException {
System.out.print("1 ");
}
}
class MyResource21 implements Closeable {
public void close() throws IOException {
throw new IOException();
}
}
try-with-resources 以 相反 的顺序关闭资源,与它们声明的顺序相反。所以代码:
- 打印
T
- try-with-resources 语句试图关闭
r2
- 抛出异常
- try-with-resources 语句成功关闭
r1
,输出1
- 异常块是运行(来自
r2
的异常)并输出IOE
- finally块是运行,输出
F
try-with-resources part of the JLS 值得一读,其中包含未解开的 try-with-resources 语句的代码示例(例如,仅包含 try
/catch
/[ 的等效代码=19=]).来自该部分:
Resources are closed in the reverse order from that in which they were initialized. A resource is closed only if it initialized to a non-null value. An exception from the closing of one resource does not prevent the closing of other resources. Such an exception is suppressed if an exception was thrown previously by an initializer, the try block, or the closing of a resource.
您的代码是
的快捷方式public static void main(String[] args) {
try {
MyResource11 r1 = new MyResource11();
try {
MyResource21 r2 = new MyResource21();
try {
System.out.print("T ");
} finally {
r2.close();
}
} finally {
r1.close();
}
} catch (IOException ioe) {
System.out.print("IOE ");
} finally {
System.out.print("F ");
}
}