gpt4 book ai didi

java - 如果发生异常,try-with-resources 会关闭资源吗?

转载 作者:行者123 更新时间:2023-11-29 07:00:06 26 4
gpt4 key购买 nike

如果发生异常,try-with-resources 是否会关闭所有打开的资源?

private void insertUserInAccessTable(int user_id) throws SQLException {
final String sql = "bla bla";
try( Connection con = ...; PreparedStatement ps = ... ) {
...
if(i==0) throw new SQLException();
}
}

最佳答案

即使会抛出异常也会被关闭。

it will be closed regardless of whether the try statement completes normally or abruptly

引用: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

关于java - 如果发生异常,try-with-resources 会关闭资源吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27561463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com