Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

java - Struts2 Exception handling usage?

I have my Action class below in which getTspNameIdMap throws ReqMgmtException exception (custom exception).

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;
    }

I know there is exception handling in Struts2 too, however presently I am not using it. Should I use Struts2 exception handling? What would be its usage?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use exception handling mechanism in Struts2, that's what exception interceptor provides. Also you should handle exceptions in the action method like in your question. If it handles all exceptions good, if not the exception handler could handle it. Also in some methods which doesn't have throws Exception signature you can only catch the exception but cannot return ERROR result. So, rethrowing the exception and handling it by the interceptor is the workaround.

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...