1

My code looks like this :

try
{
   String htmlPageText=readFromHtml("http://www.yahoo.com");
}
catch (Exception e)
{
  System.out.println("===Here===");
}

Method readFromHtml() will take a URL and return an HTML page. Normally it works fine. But I'm trying to simulate a "site down" situation, so I unplugged the Internet connection. I thought, the error should be caught and the result will be "===Here===", but instead, it returned:

java.net.UnknownHostException: http://www.yahoo.com"

and never printed out "===Here===". UnknownHostException is an extension of java.lang.Exception, so why was it not caught in the catch clause? Do I need a catch (UnknownHostException ex) to get it?

1
  • 4
    There's nothing wrong with the code you have posted. I suspect the problem lies in the readFromHtml method. Commented Feb 10, 2011 at 16:34

1 Answer 1

3

What is the readFromHTML method source code ? My guess is that this method throws some kind of exception but not UnknownHostException... Somewhere else in your code the exception is left unhandled.

Sign up to request clarification or add additional context in comments.

2 Comments

Great "catch", you got it ! I looked into it and it has this : "catch (Exception e) { e.printStackTrace(); }" in it. Thanks !
I wrote something not very accurate then. The exception was actually handled somewhere else :) It would be good if you edit your answer with the code of the method, for future bypassers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.