Siddharth Trikha
Siddharth Trikha

Reputation: 2506

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?

Upvotes: 1

Views: 1103

Answers (1)

Roman C
Roman C

Reputation: 1

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:

Upvotes: 1

Related Questions