API / Apache FreeMarker Interview questions
How do you handle exceptions raised inside a FreeMarker template using TemplateExceptionHandler?
Configuration.setTemplateExceptionHandler(handler) decides what happens the moment an error occurs while walking a template - a missing variable, a bad type conversion, a failed method call - during process().
| Handler | Behavior | Typical environment |
| RETHROW_HANDLER | Propagates the exception out of process() so calling code can catch, log, and show a proper error page | Production |
| DEBUG_HANDLER / HTML_DEBUG_HANDLER | Writes a stack trace and surrounding FTL context into the output itself | Local development only |
| IGNORE_HANDLER | Silently continues rendering, dropping the failed section | Rarely appropriate - can hide real bugs |
Beyond the built-in handlers, implementing TemplateExceptionHandler directly allows custom behavior, such as logging the failure with full context and substituting a small, user-friendly fallback string in place of the failed section instead of aborting the whole page - useful when one optional widget on a page should not be able to take down the entire response.
More Related questions...