# @jpf:catch Annotation

You can use the @jpf:catch annotation to catch exceptions that occur in the page flow and possibly route them to error handlers.

For more information about using this annotation, see Handling Exceptions in Page Flows.

## Syntax

@jpf:catch

type = "<exception type>"

{ path = "<path>" | method = "<method name>" }

[ message = "<message String>" ]

[ message-key = "<key value>" ]

## Attributes

type

Required. Specifies the fully qualified Java type of the exceptions that should be caught.

path

Either this attribute or the method attribute is required. Specifies the resource to which the runtime should forward when it catches an exception of the type specified in the type attribute. The path can map to one of the following:

- A JSP or another page flow in this web application.
- An action, such as /help/helpPage.do.

method

Either this attribute or the path attribute is required. Specifies the name of an exception handler method that has been designated with a @jpf:exception-handler annotation. That method will be invoked when the exception of the type specified in the type attribute is caught.

message

Optional. Specify a string that provides useful information about the exception. This message will be passed to the exception-handler method, if the method attribute is used.

message-key

Optional. Specify a key that is used to select a message from a message map. Typically this key is used to enable internationalization of messages, which are passed to the exception-handler method, and can also be displayed using the <netui:errors/> tag. The key refers to a message resource that is defined by the @jpf:message-resources Annotation.

## Remarks

The following rules apply to this annotation's use:

- The @jpf:catch annotation may be used on action methods and the page flow class, and in the /WEB-INF/src/global/Global.app file.

- If the type specified is a base type (for example, Exception is a base type of NullPointerException), this annotation will apply to any subtype that is thrown, unless there is a @jpf:catch that applies more specifically to the subtype.

  For example, given the following annotations, an IllegalStateException will be routed to "error1.jsp", while a NullPointerException will be routed to "error2.jsp". (Both IllegalStateException and NullPointerException are subtypes of Exception).

      	  * @jpf:catch type="Exception" path="error1.jsp"
      	  * @jpf:catch type="NullPointerException" path="error2.jsp"

- The path value can be relative to the page flow within the local file hierarchy, or relative to the root of the web application.

- In a page flow class, you can use the @jpf:catch annotation with, or without, the @jpf:exception-handler annotation. If you use @jpf:catch without @jpf:exception-handler in the page flow, you can only catch errors and take action such as forwarding to a generic errors.jsp page. For example:

      /**
       * @jpf:action
       * @jpf:forward name="readyForNextCandidate" path="name.jsp"
       * @jpf:catch type="com.acme.WorkflowException" path="errors.jsp"
      */
      public Forward confirmationPage_hire(HireForm form)
      throws Exception
      {
          hiringService.hire(firstName, lastName, title, startDate);
          .
          .
          .

- Note that @jpf:catch annotations cannot be used to catch exceptions on exception-handler methods. The following code is invalid and results in a compilation error:

>     	/**
>     	 * Invalid code:
>     	 * @jpf:exception-handler
>     	 * @jpf:catch type="Exception" path="foo.jsp"
>     	 */
>     	public Forward bad( Exception e, String msg, String msgKey, ActionForm form )

For information about using the @jpf:catch and @jpf:exception-handler annotations together, to both catch and handle exceptions, see @jpf:exception-handler annotation. Also see Handling Exceptions in Page Flows.

Related Topics

@jpf:exception-handler Annotation

Handling Exceptions in Page Flows

@jpf:action Annotation

@jpf:controller Annotation

@jpf:forward Annotation

@jpf:message-resources Annotation

@jpf:validation-error-forward Annotation

Getting Started with Page Flows