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

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

问题描述

我有Android的问题。我在设备上开发,并且捕获异常有问题。我在AsyncTask中运行一些代码,最后将其简化为:

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

我的问题是'e'变量始终为空。不知道发生了什么事。还有一点有用,但我不能说什么时候。我刚刚从电脑上起床几分钟就回来了,兴旺起来,起作用。几分钟后再编码它是空的...大约1年前有一个问题,但没有人知道答案。也许这次有人会有一些想法。





我认为这与AsyncTask有什么关系,我的异常捕获正确...仍然没有任何线索为什么:(我发现只有当debbuger连接时才会发生,当我从设备中取出电缆时,它实际上捕获并且异常不再为null ...

解决方案

在这一点上,不可能有 e 带有空值,如果您正在使用Eclipse调试应用程序,它将显示 e .toString()此时,$ code> e.toString()正在返回 null



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

  try {
if(true){
throw new Exception(ERROR);
}
}
catch(异常e){
if(e == null){
Log.e(e,e is really nu e,e不为空,toString为+ e +,消息为+ e。的getMessage());
}
}


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.

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

解决方案

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天全站免登陆