Exception Handling Provided by Struts

Exception Handling Capabilities of Struts

Target Audience: J2EE Developers, Web Component Developers

What you should know already: MVC Design Pattern, Struts Framework, Struts configuration file

Exception handling is very crucial part in Web application development. Throwing an exception is Java’s way of informing the client that something has gone wrong while doing a certain processing action.

The Struts framework provides two solutions for exception handling:

  1. Declarative Exception Handling
  2. Programmatic Exception Handling

In this blog I am going to explain about Declarative Exception Handling only.

Declarative Exception Handling

In this approach the exceptions are defined in the struts-config.xml file and in case of the exception occurs, the control is automatically passed to the appropriate error page.

The <exception> tag is used to define the exception in the struts-config.xml file. The following are the attributes of the <exception> tag.

key Defines the key present in message resource bundle file to describe the exception occurred.
type The class of the exception occurred.
path The page where the control is to be followed when an exception occurs.
handler The exception handler which will be called before passing the control to the file specified in path attribute.

The Struts framework has a default exception-handler class that is used to process the exceptions if you do not configure one of your own. The default handler class is org.apache.struts.action.ExceptionHandler. The execute( ) method of this handler creates an ActionError, stores it into the proper scope, and returns an ActionForward object that is associated with the path attribute specified in the <exception> element.

Where to define <exception> ?

There are following two sections in struts-config.xml where the exceptions can be defined:

1. With Any Action mapping: In this approach, the action class is identified which may throw an exception like password failure, resource access failure and so on. See the following code snippet:

<action path=”/login” type=”com.struts.LoginAction”>
<exception key=”error.system” type=”java.lang.Exception” path=”/index.jsp”/>

This specifies the path to which to forward when one of the specified exceptions occurs during the corresponding action invocation.

2. Defining Exceptions Globally: If the application control is to pass on a single page for all similar type of exception then the exception can be defined globally. So if in any circumstance the exception occurs the control is passed globally to the exception and control is passed to the error page. This is done using the <global-exceptions> tag :

<exception key=”error.system” type=”java.lang.Exception” path=”/index.jsp”/>

Declarative exception handling is a great addition to the struts framework which saves developers time, during both initial development and maintenance.

Permanent link to this article: https://blog.openshell.in/2010/12/exception-handling-provided-by-struts/

Leave a Reply

Your email address will not be published.