开发者

try catch exception always returns null

开发者 https://www.devze.com 2023-03-22 21:06 出处:网络
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:

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

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.

try catch exception always returns null

I th开发者_运维百科ink 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...


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


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


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!


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)

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

Another suggestion, cleaning and rebuilding the project, as explained here didn't solve it for me (but maybe for somebody else).

0

精彩评论

暂无评论...
验证码 换一张
取 消