In Java, this code throws an exception when the HTTP result is 404 range:
URL url = new URL(\"http://stackoverflow.com/asdf404notfound\"); HttpURLConnection
I know that this doesn't answer the question directly, but instead of using the HTTP connection library provided by Sun, you might want to take a look at Commons HttpClient, which (in my opinion) has a far easier API to work with.