// https://bugs.openjdk.java.net/browse/JDK-7150539 import java.net.*; public class HttpURLConnectionTests { public static class Status { boolean passed; String msg; private Status(boolean passed, String msg) { this.passed = passed; this.msg = msg; } static Status failed(String msg) { return new Status(false, msg); } static Status passed(String msg) { return new Status(true, msg); } @Override public String toString() { return (passed ? "PASSED" : "FAILED") + ": " + msg; } } public static Status getResponseCodeIOException() { URL url = null; HttpURLConnection conn = null; String host = "some_unknown_host"; int port = 12345; try { url = new URL("http://"+host+":"+port+"/index.html"); } catch(MalformedURLException e) { return Status.failed("Unexpected exception: " + e); } try { conn = (HttpURLConnection)url.openConnection(); } catch(java.io.IOException e) { return Status.failed("Unexpected exception:" + e + "; url: " + url); } int res = 0; try { res = conn.getResponseCode(); return Status.failed("getResponseCode() does not throw IOExcpetion. " + " it returns : " + res + "; url: " + url); } catch(java.io.IOException e) { } return Status.passed("OKAY"); } public static void main(String[] args) { Status s1 = getResponseCodeIOException(); System.out.println(s1); } }