Painless resource management in java -


in c++ acquiring resource in constructor , release in destructor.

so when exception rises in middle of function there no resource leak or locked mutexes or whatever.

afaik java classes don't have destructors. how 1 resource management in java.

for example:

public int foo() {         resource f = new resource();     dosomething(f);     f.release(); } 

how can 1 release resource if dosomething throws exception? can't put try\catch blocks on code, can we?

yes can , should put try/catch/finally block around code. in c# there shorthand "using" statement, in java stuck with:

public int foo() {     resource f = new resource();     try {         dosomething(f);     }     {         f.release();     } } 

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -