Reputation: 451
Java 1.7 has try-catch with resources which handles closeable resources by itself. Means that, when the try-catch block executed, resources are automatically closed.
I am confused about how try-catch block ends. Consider the following two scenarious.
Case 1:
void function()
{
try (closable)
{
doSomething();
}
catch (Exception)
{}
//at this point, the closable variable is closed
//by try-catch statement. No issues and it's clear.
}
Case 2:
void function()
{
try (closable)
{
doSomething();
return;
}
catch (Exception)
{}
//we never reached to this point, and this what
//makes me think.Is closable really closed before
//return statement or do we need to manually call
//closable.close() before returning?
}
Thanks.
Upvotes: 1
Views: 1227
Reputation: 77045
You never reach the point after the try-catch block in the second example because you return from your method. The closable will be closed though.
void close() throws Exception
Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.
While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail.
Implementers of this interface are also strongly advised to not have the close method throw InterruptedException. This exception interacts with a thread's interrupted status, and runtime misbehavior is likely to occur if an InterruptedException is suppressed. More generally, if it would cause problems for an exception to be suppressed, the AutoCloseable.close method should not throw it.
Note that unlike the close method of Closeable, this close method is not required to be idempotent. In other words, calling this close method more than once may have some visible side effect, unlike Closeable.close which is required to have no effect if called more than once. However, implementers of this interface are strongly encouraged to make their close methods idempotent.
Throws: Exception - if this resource cannot be closed
Upvotes: 0
Reputation: 344
You can use finally block for confirmation, finally block will always execute, so you can make double check at finally block:-
http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
finaly
{
if(!r.closed)
r.close();
}
Note :- finally will not work in following scenarios How to avoid a scenario in which finally block does not execute
Upvotes: 1
Reputation: 181
Try with resources is the same as using the finally block prior to JDK 7.
finally {
if (resource != null) resource.close();
}
So the answer is: The resource is clodes after leaving the try/catch/finally block.
Upvotes: 0
Reputation: 51721
Is closable really closed before return statement or do we need to manually call closable.close() before returning?
Yes, it's called for you automatically.
To verify this for yourself, you can put a breakpoint inside your closeable
object's close()
method and run you application in debug
mode. Right after the return;
executes, you should see the JVM halting its execution inside the close()
method.
Upvotes: 1
Reputation: 58929
Yes, it is closed.
This:
try(Resource r = new Resource()) {
// do stuff
}
works almost the same way as this: (it acts differently if close
throws an exception)
Resource r = new Resource();
try {
// do stuff
} finally {
r.close();
}
and finally
blocks are executed if you return from the middle of a try
block, or leave it by getting to the end, or throw an exception from inside it, or jump out of it with break
or continue
.
Upvotes: 1