即使调用了catch块,代码也在执行

Rev*_*thi 1 java

我有这个方法.问题是当满足这个条件时

if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
Run Code Online (Sandbox Code Playgroud)

它进入了这里的拦截区

catch (Exception e) {

        this.errorText = e.getMessage().toString();
        info.setErrorText(this.errorText.toString());
        response.setinfo(info);

    }
Run Code Online (Sandbox Code Playgroud)

但它仍在执行下一行

final boolean toProceedorNot = validate(bagList.toArray(new Bag[bagList.size()]))
Run Code Online (Sandbox Code Playgroud)

如果满足,那么我们想要的是什么

if (bagList==null||bagList.size()<1 && UngarList==null||UngarList.size()<1 )
**then directly return the   response;**
Run Code Online (Sandbox Code Playgroud)

这是我的方法

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;
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ton 6

为什么不执行那些行?它们在外面try/catch,没有什么能阻止正常的程序执行流程.

除非您从方法返回(或以其他方式更改控制流),否则将在catch块后面的语句处继续执行.

返回Response从catch,如果你想返回从catch块的响应块.

但是,我不相信这是一个很好用的通用目的Exception.