繁体   English   中英

Struts2异常处理用法?

[英]Struts2 Exception handling usage?

我下面有我的Action类,其中getTspNameIdMap抛出ReqMgmtException异常(自定义异常)。

public String findTspNameIdMap(){

        SlsReqMgmtCommonRemote slsReqMgmtCommonRemote = null;
        tspNameIdMap = new HashMap<String, String>();

        try{
            slsReqMgmtCommonRemote = getSlsReqMgmtCommonRemote();
            tspNameIdMap = slsReqMgmtCommonRemote.getTspNameIdMap(gmaThresholdParameters.getId().getCircleId());

        }
        catch(ReqMgmtException rEx){
            addActionError(rEx.getError());
            result = "error";
            return ERROR;
        }
        catch (Exception e){    
            addActionError("Error in processing your request. Contact Administrator");
            e.printStackTrace();
            System.out.println("[ConfigureTspThresholdAction: findTspNameIdMap Function]:In catch Inside Constructor!!");
            result = "error";
            return ERROR;
        }
        return SUCCESS;
    }

我知道Struts2中也有异常处理,但是目前我没有使用它。 我应该使用Struts2异常处理吗? 它的用途是什么?

您应该在Struts2中使用异常处理机制,这就是exception拦截器提供的功能。 同样,您应该像问题一样处理action方法中的异常。 如果它可以很好地处理所有异常,则异常处理程序可以处理它。 同样,在某些没有throws Exception签名的方法中,您只能捕获异常,而不能返回ERROR结果。 因此,重新抛出异常并由拦截器进行处理是解决方法。

参考文献:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM