java编译器说这个异常永远不会在相应的try语句的正文中抛出 - 但它_is_抛出
2022-09-03 06:48:59
我有以下代码:
try {
//jaw-ws service port operation
port.login();
} catch (Exception e) {
logger.error("Caught Exception in login(): " + e.getMessage());
}
当上述内容使用不正确的主机名运行时,我得到:
Caught Exception in login(): HTTP transport error: java.net.UnknownHostException: abc
这是正确和预期的。我重写了代码以专门捕获UnknownHostException,如下所示:
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());
}
但是,当我尝试编译它时,我得到:
[javac] foo.java: exception java.net.UnknownHostException is never thrown in body of corresponding try statement
[javac] } catch (UnknownHostException uhe) {
[javac] ^
这显然是错误的,因为异常被抛出,正如我之前捕获的那样。我在这里错过了什么?
蒂亚,卢布