java编译器说这个异常永远不会在相应的try语句的主体中抛出 - 但它_is_抛出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java compiler says this exception is never thrown in body of corresponding try statement - but it _is_ thrown
提问by rouble
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:
这是正确的和预期的。我重新编写了代码,专门捕获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());
}
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
tia, 卢布
采纳答案by BalusC
It isn't throwing an UnknownHostException
. It's just appearing in the messageof the exception you actually caught. It's likely the underlying root cause of the exception you caught.
它不是抛出一个UnknownHostException
. 它只是出现在您实际捕获的异常消息中。这可能是您捕获的异常的根本原因。
To determine the actualexception, 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:
或者只是使用Throwable#toString()
which 已经包含异常类型和消息:
} 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);
}
Updateas 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!
您绝对不应该根据消息进行区分。这是便携性问题的收据。该消息是更改的敏感主题,甚至可能取决于区域设置!
回答by Joseph Paterson
Just a thought, could it be an UnknownHostException
in a different package?
只是一个想法,它可以UnknownHostException
在不同的包装中吗?
回答by whaley
e.getMessage() is giving you output indicating that UnknownHostException is happening somewhere, but perhaps the code behind port.login() somewhere is catching UnknownHostException ,throwing another Exception, and stating in that exception's message that UnknownHostException was the original exception (or perhaps chaining it). It's hard to tell without knowing what exact Exception type is being caught in your original catch block. Find that out first (perhaps by just doing an e.printStackTrace() ) and that's should tell you what you ought to be catching.
e.getMessage() 给你的输出表明 UnknownHostException 正在某处发生,但也许 port.login() 背后的代码在某处捕获 UnknownHostException ,抛出另一个异常,并在该异常的消息中说明 UnknownHostException 是原始异常(或者也许链接起来)。如果不知道原始 catch 块中捕获的确切异常类型是什么,则很难判断。首先找出它(也许只是做一个 e.printStackTrace() ),这应该告诉你你应该捕捉什么。
回答by Stephen C
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.
回答by CamHart
On android I was seeing this but it was actually coming through as a UndeclaredThrowableException
. So instead of catching UnknownHostException
, I just caught UndeclaredThrowableException
.
在 android 上我看到了这个,但它实际上是作为UndeclaredThrowableException
. 所以UnknownHostException
我没有抓住,而是抓住了UndeclaredThrowableException
。