I want to know what is the better approach for handling exceptions. My web project is having Struts 2, Spring, and Hibernate.
An exception can occur either at Struts Action
or at Biz Layer, or at Data Layer.
How to handle exception on each layer?
I want to know what is the better approach for handling exceptions. My web project is having Struts 2, Spring, and Hibernate.
An exception can occur either at Struts Action
or at Biz Layer, or at Data Layer.
How to handle exception on each layer?
In the Struts, handling exceptions you might delegate to the framework (if you don't want to handle it manually) by defining a abstract default package that include a global exception handling.
And redirect to error_page.jsp
when this exceptions occurred.
More detailed description you could find in Exception handling in Struts 2 and Hibernate:
In the normal situation exceptions should be caught, logged and rethrown. But in the Struts2 you could handle uncaught exceptions via creating the default application interceptor stack
<interceptor-stack name="appDefaultStack"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> </interceptor-ref> </interceptor-stack>
any exceptions not caught by this application will be logged and then handled by the global exception mapping
<global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> > <global-results> <result name="error">/error_page.jsp</result> </global-results>
See How to pass exceptions globally to a single action from other actions in Struts 2.