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

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

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -