我有这个方法。 问题是当满足这个条件时
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
这里进入catch block
catch (Exception e) {
this.errorText = e.getMessage().toString();
info.setErrorText(this.errorText.toString());
response.setinfo(info);
}
但它仍然在执行下一行
final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]))
要的是满足这个条件
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
**then directly return the response;**
这是我的方法
public Response getData(Request request) {
Info info = new Info();
Response response = new Response();
String xmlrequest = request.getxmlMessage();
HashMap listMap = new HashMap();
List<Ungar> UngarList = new ArrayList<Ungar>();
List<Bag> bagList = new ArrayList<Bag>();
UniverseStaxParser xmlparser = new UniverseStaxParser();
try {
listMap = (HashMap) xmlparser.parseData(xmlrequest);
UngarList = (List<Ungar>) listMap.get("UngarItems");
bagList = (List<Bag>) listMap.get("bagItems");
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
throw new Exception("No Valid Data is passed as Input ");
} catch (Exception e) {
this.errorText = e.getMessage().toString();
info.setErrorText(this.errorText.toString());
response.setinfo(info);
}
final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]));
try {
if (!toProceedorNot) {
info.setErrorText(errorText);
response.setinfo(info);
} else {
// some logic here goes
}
} catch (Exception e) {
errorText = e.getMessage().toString();
info.setErrorText(errorText);
response.setinfo(info);
}
return response;
}
最佳答案
为什么它不执行那些行?它们在 try/catch
之外,没有什么能阻止正常的程序执行流程。
除非您从方法返回(或以其他方式改变控制流),否则将继续执行 catch
block 之后的语句。
如果要从 catch block 返回响应,请从 catch
block 返回 Response
。
不过,我不认为这是对通用Exception
的一个很好的应用。
关于java - 即使调用了 catch block ,代码仍在执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8177707/