Chapter 9. Exception and Error Handling

Introduction

Any application can shine under normal conditions; how an application responds to unexpected conditions reveals much more about its robustness and production-readiness. As you saw in Chapter 8, the Struts Validator provides a means for handling unexpected user input. However, applications need to handle unexpected system behavior. This behavior typically manifests as exceptions. These exceptions may be business-related, may have meaning to the end user, or they may be system- and coding-related, or may have meaning to system administrators and developers. This chapter will show you some good solutions for handling both of these cases.

9.1. Simplifying Exception Processing in an Action

Problem

You want to reduce the number of try . . . catch blocks within your Action classes.

Solution

Remove the exception-handling code from your Action, and define global and local exception handlers in your struts-config.xml file, as shown in Example 9-1.

Example 9-1. Global and local exception handling (partial)
... <global-exceptions> <exception key="error.unknown.user" type="com.oreilly.strutsckbk.ch09.UnknownUserException" path="/securityError.jsp"/> </global-exceptions> ... <action-mappings> <action path="/Login" type="com.oreilly.strutsckbk.ch09.LoginAction" scope="request" name="LoginForm" validate="true" input="/login.jsp"> <exception key="error.password.match" type="com.oreilly.strutsckbk.ch09.PasswordMatchException"> <forward name="success" ...

Get Jakarta Struts Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.