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

22,408

Solution 1

It isn't throwing an UnknownHostException. It's just appearing in the message of the exception you actually caught. It's likely the underlying root cause of the exception you caught.

To determine the actual exception, you should print a bit more detail. E.g.

} catch (Exception e) {
    logger.error("Caught Exception in login(): " + e.getClass().getName() + ": " + e.getMessage());
}

or just using Throwable#toString() which already includes both exception type and message:

} catch (Exception e) {
    logger.error("Caught Exception in login(): " + e);
}

or just pass the Exception in as 2nd logger argument, if well configured its stacktrace will be printed:

} catch (Exception e) {
    logger.error("Caught Exception in login(): " + e.getMessage(), e);
}

Update as per your comments: your best bet is to update the catch as follows:

} catch (ClientTransportException e) {
    if (e.getCause() instanceof UnknownHostException) {
        // UHE.
    } else {
        // Other.
    }
}

You should absolutely not differentiate based on the message. It's receipe for portability trouble. The message is namely a sensitive subject for changes which may even be locale dependent!

Solution 2

There are various subtle (or in some cases unsubtle) ways to throw a checked exceptions in situations where they are not explicitly declared. Some are listed in this page.

Share:
22,408
rouble
Author by

rouble

Dad of twins. Boilermaker. Tech guy. Coffee addict. Home automation hobbyist. Might very well be an alien.

Updated on October 30, 2020

Comments

  • rouble
    rouble over 3 years

    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

  • zneak
    zneak almost 14 years
    No. The message clearly states java.net.UnknownHostException.
  • rouble
    rouble almost 14 years
    yep - you're right, the actual exception is com.sun.xml.internal.ws.client.ClientTransportException: HTTP transport error: java.net.NoRouteToHostException: No route to host which is a proprietary class, so I'll just differentiate those based on the message in catch (Exception e)
  • whaley
    whaley almost 14 years
    Checking the contents of the message string of an Exception to determine what to do sounds kind of dirty. It looks like ClientTransportException is a RuntimeException, so why not write a separate catch block for RuntimeExceptions if you want to treat them differently.
  • Stephen C
    Stephen C almost 9 years
    Fixed. The page moved.