I have this method .
The issue is that when this condition is met
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
It is going into the catch block here
catch (Exception e) {
this.errorText = e.getMessage().toString();
info.setErrorText(this.errorText.toString());
response.setinfo(info);
}
But still it is execuing the next lines that is
final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]))
what is want is if this is met
if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
**then directly return the response;**
This is my method
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;
}
Why wouldn’t it execute those lines? They’re outside the
try/catch, and nothing prevents normal program execution flow.Unless you return from the method (or otherwise alter control flow) execution will continue at the statement following the
catchblock.Return the
Responsefrom thecatchblock if you want to return the response from the catch block.I’m not convinced that this is a great use of a general-purpose
Exception, however.