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

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:
```
```
public int foo() {
    Resource f = new Resource();
    try {
        DoSomething(f);
    }
    finally {
        f.Release();
    }
}
```
```