X x = null; Y y = null; try{ x = foo(); y = bar(); yadda(x,y); } finally { if (x!=null) x.dispose(); if (y!=null) y.dispose(); }
if (x != null) try { x.dipose(); } catch (Exception){} }
it's why the using keyword is so nice. Still, this is all hoops languages force us to deal with when they shouldn't (which is the OPs point)
Not lucky enough, because x.dispose() could throw an unchecked exception.
Util.dispose(x,y);
public void withConnection(Callback<Connection> callback) { Connection connection = createConnection(); try { callback.call(connection); } finally { connection.dispose(); } }
Once Java actually gets closures it'll make this soooo much nicer.
if (x != null) try { x.dipose(); } catch (Exception){} }
it's why the using keyword is so nice. Still, this is all hoops languages force us to deal with when they shouldn't (which is the OPs point)