title: java file handling and exceptions
body: <p>The standard way of handling file reading and writing in java goes something like this:</p>
  
  <pre><code>try
  {
      ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.dat"));
      oos.writeObject(h);
      oos.close();
  }
  catch (FileNotFoundException ex)
  {
  }
  catch (IOException ex)
  {
  }
  </code></pre>
  
  <p>But I'm bothered by that code, because it could be possible here that the file is never closed if an exception is thrown. Sure we could add a finally clause and initialise the ObjectOutputStream outside the try block. However, when you do that you need to add another try/catch block INSIDE the finally block again...that's just ugly. Is there a better way of handling this problem?</p>