title: Closing URLConnection and InputStream correctly?
body: <p>I've seen many different examples of using HttpURLConnection + InputStream, and closing them (or not closing them) after use. This is what I came up with to make sure everything is closed after finished, whether there's an error or not. Is this valid?:</p>
  
  <pre><code>HttpURLConnection conn = null;
  InputStream is = null;
  try {
      URL url = new URL("http://example.com");
  
      // (set connection and read timeouts on the connection)
      conn = (HttpURLConnection)url.openConnection();
  
      is = new BufferedInputStream(conn.getInputStream());
  
      doSomethingWithInputStream(is);
  
  } catch (Exception ex) {
  } finally {
      if (is != null) {
          try {
              is.close();
          } catch (IOException e) {
          }
      }
      if (conn != null) {
          conn.disconnect();
      }
  }
  </code></pre>
  
  <p>Thanks</p>