patvax
patvax

Reputation: 624

Java - Is a Connection/Stream already closed if an Exception occurs and how to handle it properly?

I am wondering, if I have some sort of Connection or Stream in my code that should be closed to release ressources, what does it mean for the connection instance itself?

Code example:

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?
    }
}

For example if I have an opened TCP-Connection to let's say an SQL-Server and I am unable to close it because the server has crushed or my device can't reach the device anymore. I would obviously get an IO or in this case an SQLException. If so:

  1. Should I consider the resource or socket to actually have benn released?
  2. Will the JVM or the OS handle it overtime? (the OS would probablly do that eventually but it's about what is actually a good programming practice in this case)
  3. Should I handle the issue myself by trying

Edit 1: I am aware of the "try with resources" construct. I only just wonder how to handle a connection closing if the Connection I am using does not implement AutoCloseable.

Upvotes: 1

Views: 816

Answers (1)

Jacob G.
Jacob G.

Reputation: 29680

I would have CustomConnection implement the AutoClosable interface so that it can be used within a try-with-resources statement:

try (CustomConnection connection = ...) {

} catch (IOException e) {
    // connection is not in scope here, and it is closed.
}

From Oracle's try-with-resources tutorial, it states:

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.

Using this to answer your question, your resources will be closed upon entering either a catch or finally block.


If initializing the connection can throw an exception, you'll have to watch out for suppressed exceptions:

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.

Upvotes: 1

Related Questions