Java - 如果发生异常,Connection/Stream 是否已经关闭以及如何正确处理?
Java - Is a Connection/Stream already closed if an Exception occurs and how to handle it properly?
我想知道,如果我的代码中有某种应该关闭以释放资源的连接或流,这对连接实例本身意味着什么?
代码示例:
CustomConnection connection;
try{
connection = //some code that opens a connection
//some code using the connection
}catch(IOException){
//Some logging and Handling of IOExceptions
}finally{
//resources cleanup
try{
connection.close();
}catch(IOException){
//Some Logging
//What else should be done here?
//Is my Connection closed at this point and can I just move on?
//Or should there be anything else done here
//to ensure that the connection is actually closed?
}
}
例如,如果我有一个打开的 TCP 连接,比方说一个 SQL-服务器,我无法关闭它,因为服务器已损坏或我的设备无法再连接到该设备。我显然会得到一个 IO,或者在这种情况下会得到一个 SQLException。如果是:
- 我是否应该考虑资源或套接字实际上有 benn
已发布?
- JVM 或 OS 会超时处理吗? (OS 最终可能会这样做,但在这种情况下,它实际上是一个好的编程实践)
- 我是否应该尝试自己处理问题
编辑 1:我知道 "try with resources" 结构。我只是想知道如果我使用的连接未实现 AutoCloseable,如何处理连接关闭。
我会让 CustomConnection
实现 AutoClosable
接口,以便它可以在 try-with-resources 语句中使用:
try (CustomConnection connection = ...) {
} catch (IOException e) {
// connection is not in scope here, and it is closed.
}
来自 Oracle 的 try-with-resources tutorial,它指出:
Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.
使用此回答您的问题,您的资源将在进入 catch
或 finally
块时关闭。
如果初始化连接会抛出异常,则必须注意抑制的异常:
An exception can be thrown from the block of code associated with the try-with-resources statement. In the example writeToFileZipFileContents, an exception can be thrown from the try block, and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the ZipFile and BufferedWriter objects. If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the writeToFileZipFileContents method. You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.
我想知道,如果我的代码中有某种应该关闭以释放资源的连接或流,这对连接实例本身意味着什么?
代码示例:
CustomConnection connection;
try{
connection = //some code that opens a connection
//some code using the connection
}catch(IOException){
//Some logging and Handling of IOExceptions
}finally{
//resources cleanup
try{
connection.close();
}catch(IOException){
//Some Logging
//What else should be done here?
//Is my Connection closed at this point and can I just move on?
//Or should there be anything else done here
//to ensure that the connection is actually closed?
}
}
例如,如果我有一个打开的 TCP 连接,比方说一个 SQL-服务器,我无法关闭它,因为服务器已损坏或我的设备无法再连接到该设备。我显然会得到一个 IO,或者在这种情况下会得到一个 SQLException。如果是:
- 我是否应该考虑资源或套接字实际上有 benn 已发布?
- JVM 或 OS 会超时处理吗? (OS 最终可能会这样做,但在这种情况下,它实际上是一个好的编程实践)
- 我是否应该尝试自己处理问题
编辑 1:我知道 "try with resources" 结构。我只是想知道如果我使用的连接未实现 AutoCloseable,如何处理连接关闭。
我会让 CustomConnection
实现 AutoClosable
接口,以便它可以在 try-with-resources 语句中使用:
try (CustomConnection connection = ...) {
} catch (IOException e) {
// connection is not in scope here, and it is closed.
}
来自 Oracle 的 try-with-resources tutorial,它指出:
Note: A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.
使用此回答您的问题,您的资源将在进入 catch
或 finally
块时关闭。
如果初始化连接会抛出异常,则必须注意抑制的异常:
An exception can be thrown from the block of code associated with the try-with-resources statement. In the example writeToFileZipFileContents, an exception can be thrown from the try block, and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the ZipFile and BufferedWriter objects. If an exception is thrown from the try block and one or more exceptions are thrown from the try-with-resources statement, then those exceptions thrown from the try-with-resources statement are suppressed, and the exception thrown by the block is the one that is thrown by the writeToFileZipFileContents method. You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.