I have seen two ways of acquiring and disposing resources. Either:
Resource resource = getResource();
try { /* do something with resource */ }
finally { resource.close(); }
or:
Resource resource = null;
try { resource = getResource(); /* do something with resource */ }
finally { if (resource != null) resource.close(); }
I was wondering which style is preferable. The first one avoids the if
condition, while the second one (I presume) handles the case of thread abort right after the assignment but before entering the try
block. What other pros and cons do these styles have over each other? Which one should I preferably use?