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.