Freemarker comes with a number of handlers for when an exception occurs in a template. You can also write your own handler for custom circumstances. This is all detailed in the Freemarker docs.

Selecting an exception handler

Selection can be made through a freemarker.properties file in the root of you classpath (i.e. next to your xwork.xml). The property to set is

template_exception_handler=rethrow

The pre-defined choices are:

  • debug - Prints stack trace (includes FTL error message and FTL stack trace) and re-throws the exception.
  • html_debug (default) - Same as debug, but it formats the stack trace so that it will be readable with Web browsers.
  • ignore - Simply suppresses all exceptions (but remember, FreeMarker will still log them). It does nothing to handle the event. It does not re-throw the exception.
  • rethrow - Simply re-throws all exceptions, it doesn't do anything else. This handler can be good for Web applications (assuming you don't want to continue template processing after exception), because it gives the most control to the Web application over page generation on error conditions (since FreeMarker doesn't print anything to the output about the error).

Descriptions taken from the Freemarker docs.

If you wish to use a custom handler then you provide your fully qualififed class name.

Rethrowing Exceptions

For a production system it is often a good choice to use the rethrow handler. This prevents your stack trace being displayed to the user and allows you to present a friendly error screen.

As of WebWork 2.2.6 when the rethrow handler is active then all output is buffered until the template is fully processed. This prevents parts of the template processed before an exception appearing in the view. If processing completes then the output is flushed to the writer. If an exception occurs then it is thrown out of the Freemarker Result Type and processed by the Exception Interceptor. See Exception Handling for details on how to present a friendly error screen.

If you are using a custom result handler then the FreemarkerResultType provides a parameter bufferOutput which when set to true buffers all output, preventing unwanted content being written.