【发布时间】:2014-04-21 07:39:25
【问题描述】:
我正在开发一个使用以下 sn-p 的 android 应用程序:
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;
}
}
在我调试代码时。最后一个catch块return false;中的return语句在不执行this.e = e;的情况下执行,this.e是null。如果我删除最后一个 catch 块
catch (Exception e) {
this.e = e;
return false;
}
然后执行后面的catch块中的return语句
catch (XmlPullParserException e) {
this.e = e;
return false;
}
我犯了什么错误?
【问题讨论】:
-
执行这段代码时调用了哪个异常
-
捕获
Exception通常是个坏主意,因为您可能会捕获一些与您不对应的Exception,因此您应该始终只捕获您知道try的特定异常块可以抛出。这可能就是这里发生的情况,您输入的是Exception,因为它比XmlPullParserException更通用。 -
你想达到什么目的??
-
你的意思是说当你捕获异常时,this.e 仍然为空??
-
@Amith GC。没错,你是对的。我需要做什么?