Question

I have a Stateless Session Bean:

public CmcMapServerInfo getMapServerDetails() throws IllegalArgumentException, PersistenceException{

        Query query;
        List<CmcMapServerInfo> resultList;
        CmcMapServerInfo mapServer = null;

        try{
            query = em.createNamedQuery("CmcMapServerInfo.getMapServerr");
            resultList = query.getResultList();
            mapServer =  resultList.get(0); // as only single row is there
        }catch(IllegalArgumentException e){
            System.out.println(" ILLEGAL CATCHED------------------------------------------------------------");
            throw e;
        }catch(PersistenceException e){
            System.out.println(" PERSISTENCE CATCHED------------------------------------------------------------");
            throw e;
        }
        return mapServer;
    }

Here CmcMapServerInfo.getMapServerr is illegal argument, thus an IllegalArgumentException is catched here and rethrown to the Action class where the this function was called.

Action class:

public String getMapServerDetails(){

        try{
            mapServer = getSlsConfigureMapServerRemote().getMapServerDetails();
        }catch(NamingException e){
            System.out.println("NAMING EXCEPTION INSIDE :---------------");
            addActionError("There was an Error displaying the Required Page. Please Contact the system Adminstrator");
            return ERROR;
        }catch(IllegalArgumentException e){
            System.out.println("ILLEGAl ARG INSIDE :---------------");
            addActionError("There was an Error getting the Map Server Details. Please Contact the system Adminstrator");
            return ERROR;
        }catch(PersistenceException e){
            System.out.println("PERSISTENCE INSIDE :---------------");
            addActionError("There was an Error getting the Map Server Details. Please Contact the system Adminstrator");
            return ERROR;
        }
        return SUCCESS;
    }

The problem is here inside the Action class the rethrown exception from bean is not caught, thus struts2 exception Interceptor handles it.

I have configured struts2 exception handling in my struts.xml:

<package name="cdot.oss.cmsat.conf.mapserver.struts" namespace="/"
        extends="struts-default">

        <interceptors>
            <interceptor-stack name="appDefaultStack">
                <interceptor-ref name="defaultStack">
                    <param name="exception.logEnabled">true</param>
                    <param name="exception.logLevel">ERROR</param>
                </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="appDefaultStack" />

        <global-results>
            <result name="exception">mapServer/pages/Exception.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="exception" />
        </global-exception-mappings>


        <action name="getMapServerDetails"
            class="cdot.oss.cmsat.conf.struts.ConfigureMapServerAction" method="getMapServerDetails">

            <result name="success">mapServer/pages/ConfigureMapServerInput.jsp
            </result>
            <result name="input">mapServer/pages/ConfigureMapServerInput.jsp
            </result>
            <result name="error">mapServer/pages/Error.jsp</result>


        </action>

Since exception is not caught inside Action class, the global exception mapping of Exception handles it and displays the Exception.jsp.

Why is the exception not catched in Action class even though it's catched in Bean and rethrown there?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top