News for hackers. Stuff that matters.
Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by JulianMorrison on Friday May 06, 2011 @11:55AM (#2521293)
I think you can avoid nesting finally thus:

    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();
    }
Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by latch on Friday May 06, 2011 @12:09PM (#2521343)
what happens if x.dispose() throws an exception?

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)

Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by JulianMorrison on Friday May 06, 2011 @12:20PM (#2521386)
You're lucky Java is checked at compile time. Ruby would eat that "no such method dipose" and silently leak x.
Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by CodeMage on Friday May 06, 2011 @12:25PM (#2521410)
You're lucky Java is checked at compile time.

Not lucky enough, because x.dispose() could throw an unchecked exception.

Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by mynameishere on Friday May 06, 2011 @01:46PM (#2521761)
I was thinking the same thing, but replace your finally block with,

  Util.dispose(x,y);
...and let that handle all the possible issues.
Re:Exception Safety, Garbage Collection etc. (a.k.a. Why Doe... by reverend_gonzo on Friday May 06, 2011 @02:04PM (#2521840)
I've taken Groovy's with... syntax and built utils that are ie:

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.