title: How do I close a socket or an I/O Stream in the windowClosing event of a Java window?
body: <p>I'm having trouble closing my socket when exiting my Java application. I thought that an easy way to make sure the socket gets closed is to hook it on windowClosing in a Swing JFrame like this: </p>
  
  <pre><code>    frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
              input.close();
              output.close();
              socket.close();
  
              frame.dispose();
              System.exit(0);
          }
      });
  </code></pre>
  
  <p>But then I have to handle the IOException that close() creates, and I can't throw it when overriding the event method like that. </p>
  
  <p>How can I make sure that my streams and my socket get closed when the program does?</p>
  
  <p>Thanks.</p>