java compiler says this exception is never thrown in body of corresponding try statement - but it _is_ thrown

Viewed 22516

I have the following code:

try {
    //jaw-ws service port operation
    port.login();
} catch (Exception e) {
    logger.error("Caught Exception in login(): " + e.getMessage());
}

When the above is run with an incorrect hostname, I get:

Caught Exception in login(): HTTP transport error: java.net.UnknownHostException: abc

That is correct and expected. I re-wrote the code to specifically catch UnknownHostException, as follows:

import java.net.UnknownHostException;
try {
    //jaw-ws service port operation
    port.login();
} catch (UnknownHostException uhe) {
    //do something specific to unknown host exception
} catch (Exception e) {
    logger.error(Caught Exception in login(): " + e.getMessage());
}

However, when I try to compile this I get:

[javac] foo.java: exception java.net.UnknownHostException is never thrown in body of corresponding try statement
[javac]         } catch (UnknownHostException uhe) {
[javac]                  ^

This is clearly false, since the exception is thrown, as I have caught it before. What am I missing here?

tia, rouble

5 Answers
Related