java try catch 异常总是返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6834106/
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
try catch exception always returns null
提问by mariozski
I got problem with Android. I develop on device and have problem with catching exceptions. I'm running some code in AsyncTask and finally simplified it to:
我在 Android 上遇到了问题。我在设备上开发并且在捕获异常时遇到问题。我在 AsyncTask 中运行了一些代码,最后将其简化为:
try
{
if (true)
throw new Exception("ERROR");
}
catch (Exception e)
{
Log.e("e", "exception", e);
}
My problem is that 'e' variable is always null. Not sure what's happening actually. What's more it sometimes works, but I can't say when. I just get up from computer for few minutes come back and boom, it works. Doing coding few minutes and again it's null... There was one question on SO about 1 year ago but noone known answer. Maybe this time someone will have some idea.
我的问题是“e”变量始终为空。不确定实际发生了什么。更重要的是它有时有效,但我不能说什么时候。我刚从电脑上起床几分钟,然后砰的一声,它起作用了。编码几分钟又一次它是空的......大约一年前有一个关于 SO 的问题,但没有人知道答案。也许这一次有人会有一些想法。
I think that it have something to do with AsyncTask as outside of it, I got exception catched properly... still don't have any clue why :( I found it only happens when debbuger is connected. When I take out cable from device it actually catches and exception isn't null anymore...
我认为它与 AsyncTask 有关,因为在它之外,我正确捕获了异常......仍然不知道为什么:( 我发现它只在连接调试器时发生。当我从设备中取出电缆时它实际上捕获并且异常不再为空...
回答by Italo Borssatto
It's impossible to have e
with null value at that point. If you are debugging your app using Eclipse, it will show the e.toString()
value at that point and the e.toString()
is returning null
.
那时不可能e
有空值。如果您正在使用 Eclipse 调试您的应用程序,它将显示该e.toString()
点的值并e.toString()
返回null
.
Try another test, using this code:
尝试另一个测试,使用以下代码:
try {
if (true) {
throw new Exception("ERROR");
}
}
catch (Exception e) {
if (e == null) {
Log.e("e", "e is really null!!!");
}
else {
Log.e("e", "e is not null, toString is " + e + " and message is " + e.getMessage());
}
}
回答by Vitor M. Barbosa
I know this is an old question, but this happened with me as well, and it seems like the problem is with the debugger itself!
When I run my app from eclipse (ctrl+ F11), it catches a proper exception (e != null
).
我知道这是一个老问题,但这也发生在我身上,似乎问题出在调试器本身!
当我从 eclipse ( ctrl+ F11)运行我的应用程序时,它会捕获一个正确的异常 ( e != null
)。
回答by ibanezshredd
I had this same problem.... I found it was the debugger like the other people had said. So what I did to debug is to just put the break point on the first line of the catch block, rather than right on the catch block. Seemed to work for me!
我有同样的问题......我发现它是其他人所说的调试器。所以我做的调试只是把断点放在catch块的第一行,而不是放在catch块上。似乎对我有用!
回答by yonojoy
I had exactly the same strange problem inside an AsyncTask, while debugging on a real device (Galaxy Tab 2). (And yes, I got "e is really null!!!"
doing the test suggested by @italo)
在真实设备(Galaxy Tab 2)上调试时,我在 AsyncTask 中遇到了完全相同的奇怪问题。(是的,我"e is really null!!!"
做了@italo 建议的测试)
For me the problem mysteriously went away after unplugging the usb plug of the android device and connecting it again afterwards (and then running my app again).
对我来说,在拔下 android 设备的 USB 插头并再次连接(然后再次运行我的应用程序)后,问题神秘地消失了。
Another suggestion, cleaning and rebuilding the project, as explained heredidn't solve it for me (but maybe for somebody else).