try catch 异常总是返回 null [英] try catch exception always returns null

查看:89
本文介绍了try catch 异常总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Android 时遇到了问题.我在设备上开发并且在捕获异常时遇到问题.我在 AsyncTask 中运行了一些代码,最后将其简化为:

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:

try
{
    if (true)
        throw new Exception("ERROR"); 
}      
catch (Exception e)
{    
    Log.e("e", "exception", e);
}

我的问题是e"变量始终为空.不确定实际发生了什么.更重要的是它有时有效,但我不能说什么时候.我刚从电脑上起床几分钟,然后砰的一声,它起作用了.编码几分钟又一次它是空的......大约一年前有一个关于 SO 的问题,但没有人知道答案.或许这一次有人会有一些想法.

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.

我认为它与 AsyncTask 有关,因为在它之外,我正确捕获了异常......仍然不知道为什么 :( 我发现它只在连接调试器时发生.当我取出时来自它实际捕获的设备的电缆,并且异常不再为空...

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...

推荐答案

在那个时候 e 不可能有空值.如果您正在使用 Eclipse 调试您的应用程序,它将在该点显示 e.toString() 值并且 e.toString() 返回 null.

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.

尝试另一个测试,使用以下代码:

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());
    }
}

这篇关于try catch 异常总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆