在catch块内调试return语句 [英] Debugging return statement within catch block

查看:126
本文介绍了在catch块内调试return语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,其中使用以下代码段:

  private Boolean myMethod(boolean isOnline){
try {
if(isOnline){
...
return true;
}
else {
...
return true;
}
}
catch(SocketTimeoutException e){
this.e = e;
返回false;
} catch(IOException e){
this.e = e;
返回false;
} catch(XmlPullParserException e){
this.e = e;
返回false;
} catch(Exception e){
this.e = e;
返回false;
}
}

当我调试代码。在执行 this.e = e; this.e null 。如果我删除最后一个catch块

  catch(Exception e){
this.e = e;
返回false;
}

然后执行以下catch块中的return语句

  catch(XmlPullParserException e){
this.e = e;
返回false;
}

我犯了什么错误?

解决方案

您的错误是添加此块:

  catch(Exception e){
this.e = e;
返回false;
}

它将捕获所有内容,导致您的XmlPullParserException不被称为IMO。 p>

I'm developing an android application where I use the following snippet:

private Boolean myMethod(boolean isOnline) {
    try {
        if (isOnline) {
            ...
            return true;    
        } 
        else {
            ...
            return true;
        }
    } 
    catch (SocketTimeoutException e) {
        this.e = e;
        return false;
    } catch (IOException e) {
        this.e = e;
        return false;
    } catch (XmlPullParserException e) {
        this.e = e;
        return false;
    } catch (Exception e) { 
        this.e = e;
        return false;
    }
}

while I'm debugging the code. The return statement in the last catch block return false; is executed without executing this.e = e; and this.e is null. If I remove the last catch block

catch (Exception e) { 
    this.e = e;
    return false;
}

then the return statement in the following catch block is executed

catch (XmlPullParserException e) {
    this.e = e;
    return false;
} 

What mistakes did I make?

解决方案

Your mistake is adding this block:

catch (Exception e) { 
    this.e = e;
    return false;
}

It will catch everything, leading to your XmlPullParserException not being called IMO.

这篇关于在catch块内调试return语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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