<p>In C++ we acquiring a resource in a constructor and release it in a destructor.</p>

<p>So when an exception rises in a middle of a function there will be no resource leak or locked mutexes or whatever.</p>

<p>AFAIK java classes don't have destructors. So how does one do the resource management in Java.</p>

<p>For example:</p>

<pre><code>public int foo() {    
    Resource f = new Resource();
    DoSomething(f);
    f.Release();
}
</code></pre>

<p>How can one release resource if DoSomething throws an exception? We can't put try\catch blocks all over the code, can we?</p>

[{'answer_id': 58552, 'author': 'qbeuek', 'author_id': 5348, 'author_profile': 'https://Stackoverflow.com/users/5348', 'pm_score': 3, 'selected': True, 'text': '<p>Yes you can and should put try/catch/finally block around your code. In C# there is a shorthand "using" statement, but in Java you are stuck with:</p>\n\n<pre><code>public int foo() {\n    Resource f = new Resource();\n    try {\n        DoSomething(f);\n    }\n    finally {\n        f.Release();\n    }\n}\n</code></pre>\n'}, {'answer_id': 58563, 'author': 'svrist', 'author_id': 86, 'author_profile': 'https://Stackoverflow.com/users/86', 'pm_score': 0, 'selected': False, 'text': '<p>If you want the using block get involved in the java closure debate :S</p>\n'}, {'answer_id': 58573, 'author': 'Spoike', 'author_id': 3713, 'author_profile': 'https://Stackoverflow.com/users/3713', 'pm_score': -1, 'selected': False, 'text': '<p>Sorry to disappoint you but in Java we <strong>do</strong> use try\\catch\\finally blocks a lot. And with "a lot", I mean <strong>A LOT</strong>. I do sometimes wish that Java has the C# using block. Most of the time you won\'t need to free up resources as Java\'s garbage collector will take care of that.</p>\n\n<p>However exceptions do have their uses in making error handling a lot cleaner. You can write your own exceptions and catch them for whatever you are doing. No more returning arbitrary error codes to the user!</p>\n'}, {'answer_id': 58817, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 2, 'selected': False, 'text': '<p>It is possible to factor out try/finally (and exception and algorithms) using the Execute around idiom. However the syntax is highly verbose.</p>\n\n<pre><code>public int foo() {\n    withResource(new WithResource() { public void run(Resource resource) {\n        doSomething(resource);\n    }});\n}\n\n...\n\npublic interface WithResource {\n    void run(Resource resource);\n}\n\npublic static void withResource(WithResource handler) {\n    Resource resource = new Resource();\n    try {\n        handler.run(resource);\n    } finally {\n        resource.release();\n    }\n}\n</code></pre>\n\n<p>This sort of thing makes more sense if you are abstracting more than try/finally. For instance, with JDBC you can execute a statement, loop through the results, close resources and wrap the exception.</p>\n'}, {'answer_id': 7866351, 'author': 'Raedwald', 'author_id': 545127, 'author_profile': 'https://Stackoverflow.com/users/545127', 'pm_score': 2, 'selected': False, 'text': '<p>This question dates to 2008 and therefore pertains to Java 6. Since then Java 7 has been released, which contains a new feature for Automatic Resource Management. For a more recent question that is relevant to Java 7 see this question:</p>\n\n<p><a href="https://stackoverflow.com/questions/6628611/java-techniques-for-automatic-resource-release-prompt-cleanup">java techniques for automatic resource release? &quot;prompt cleanup&quot;?</a></p>\n'}]
['https://Stackoverflow.com/questions/58547', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1007/']