1

Don't know if I'm the first one to run into this, but I'll post it here to save someones time. Ok, so after playing around and trying out HttpURLConnection to do some HTTP requests from Android I wondered what would happen if I used it with Internet on my phone disabled. I've stumbled on an interesting bug:

    URL url = new URL("http://google.com");
    HttpURLConnection c = (HttpURLConnection) url.openConnection();
    c.setReadTimeout(15000);
    try {
        InputStream in = new BufferedInputStream(c.getInputStream());
        httpResult = readStream(in);
    } catch (IOException e) {
        Log.e(TAG, "Error: ", e);

    } finally {
        c.disconnect();
    }

If I run this code with Internet disabled on my phone an IOException gets caught with no stacktrace whatsoever. What's happenning?

VM4
  • 6,321
  • 5
  • 37
  • 51

1 Answers1

3

So after some digging around I found that a subtype of IOException is actually being thrown: UnknownHostException. Android documentation for HttpURLConnection.getInputStream() and HttpURLConnection.connect() say nothing about it.

VM4
  • 6,321
  • 5
  • 37
  • 51
  • it's called specialization. `connect` declares that it throws *an* IOException, then the details are given by the specialized implementation. – njzk2 Dec 03 '13 at 21:19
  • Hmm, I've only looked here http://developer.android.com/reference/java/net/URLConnection.html#connect() Where can I find the specialized implementation? – VM4 Dec 03 '13 at 21:24
  • in http://developer.android.com/reference/java/io/IOException.html sections Known direct subclasses and Known indirect subclasses. Anyone who throws an IOException can throw any of those 53 classes. – njzk2 Dec 03 '13 at 21:48