API / Apache FreeMarker Interview questions
What happens when a variable referenced in a template is not found in the data model?
Referencing an undefined variable and then trying to actually use it - printing it, comparing it, calling a method on it - raises an InvalidReferenceException, a subtype of TemplateException, which by default aborts processing of that template.
What happens next depends on the TemplateExceptionHandler configured on the Configuration:
| Handler | Behavior |
| RETHROW_HANDLER | Propagates the exception out of process(); recommended for production so the caller can log it and show an error page |
| DEBUG_HANDLER | Prints a stack trace and FTL context into the output stream; useful only in development |
| HTML_DEBUG_HANDLER | Same as DEBUG_HANDLER but HTML-formatted |
| IGNORE_HANDLER | Swallows the error and continues; risky, since it can silently hide bugs |
Guarding the reference with ?? or supplying a default with ! avoids triggering the exception in the first place, which is the normal way to handle genuinely optional data.
More Related questions...