Java 即使调用了catch块,代码仍在执行
我有这个方法。 问题是,当满足这一条件时Java 即使调用了catch块,代码仍在执行,java,Java,我有这个方法。 问题是,当满足这一条件时 if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 ) 但它仍然在执行下一行 final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()])) 我们想要的是,如果这一点得到满足 if (bagList==null||bagList
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
但它仍然在执行下一行
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;**
if(bagList==null | | bagList.size()为什么不执行这些行?它们在try/catch
之外,没有任何东西阻止正常的程序执行流
除非从方法返回(或以其他方式更改控制流),否则执行将在catch
块后面的语句处继续
如果要从catch块返回响应,请从catch块返回响应
但是,我不认为这是对通用异常的一种很好的使用。为什么它不执行这些行呢?它们在try/catch
之外,没有任何东西可以阻止正常的程序执行流
除非从方法返回(或以其他方式更改控制流),否则执行将在catch
块后面的语句处继续
如果要从catch块返回响应,请从catch块返回响应
但是,我不认为这是对通用异常的一种很好的使用。我认为您应该重新设计软件的这一部分:
if(bagList==null | | bagList.size()我认为您应该重新设计软件的这一部分:
if(bagList==null | | bagList.size()调用null对象的方法将引发null指针异常。这是一个常见的错误。非常感谢大家,我已经修改了代码,将其保存在一个try块下,现在可以工作了。调用null对象的方法将引发null指针异常。这是一个常见的错误。非常感谢大家,我已经修改了代码把它放在一个试块下,现在它可以工作了。
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;
}
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )