title: What are different ways to handle error in FreeMarker template?
body: <p>How to suppress FreeMarker template error?
  I am looking here: <a href="http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html" rel="noreferrer">http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html</a>
  But I do not understand how to "TemplateExceptionHandler.IGNORE_HANDLER." I am using Struts2 and also how to show another ftl page instead of showing the stack trace?</p>
  
  <pre><code>class MyTemplateExceptionHandler implements TemplateExceptionHandler {
      public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out)
              throws TemplateException {
          try {
              out.write("[ERROR: " + te.getMessage() + "]");
          } catch (IOException e) {
              throw new TemplateException("Failed to print error message. Cause: " + e, env);
          }
      }
  }
  
  ...
  
  cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
  </code></pre>
  
  <p>Found the above piece at <a href="http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html" rel="noreferrer">http://freemarker.sourceforge.net/docs/pgui_config_errorhandling.html</a>
  How do I use this? That last line, where does cfg come from?</p>
  
  <p>"Main entry point into the FreeMarker API"... <a href="http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html" rel="noreferrer">http://massapi.com/source/freemarker-2.3.18/src/freemarker/template/Configuration.java.html</a></p>
  
  <p>So, that is the main entry point, I am guessing cfg comes from this class. I am still not seeing how the controller will come into my class MyTemplateExceptionHandler.</p>
  
  <p>Where will the following line needs to go?</p>
  
  <pre><code>cfg.setTemplateExceptionHandler(new MyTemplateExceptionHandler());
  </code></pre>
  
  <p>And is it just a matter of placing this line in correct spot?</p>
  
  <p>This is how my current class looks like:</p>
  
  <pre><code>    import java.io.File;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.Writer;
  import java.util.Properties;
  
  import freemarker.cache.FileTemplateLoader;
  import freemarker.cache.MultiTemplateLoader;
  import freemarker.cache.TemplateLoader;
  import freemarker.cache.WebappTemplateLoader;
  import freemarker.core.Environment;
  import freemarker.ext.beans.BeansWrapper;
  import freemarker.ext.jsp.TaglibFactory;
  import freemarker.ext.servlet.HttpRequestHashModel;
  import freemarker.ext.servlet.HttpRequestParametersHashModel;
  import freemarker.ext.servlet.HttpSessionHashModel;
  import freemarker.ext.servlet.ServletContextHashModel;
  import freemarker.template.ObjectWrapper;
  import freemarker.template.TemplateException;
  import freemarker.template.TemplateExceptionHandler;
  import freemarker.template.TemplateModel;
  
  import javax.servlet.GenericServlet;
  import javax.servlet.ServletContext;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.apache.struts2.views.JspSupportServlet;
  import org.apache.struts2.views.freemarker.FreemarkerManager;
  import org.apache.struts2.views.freemarker.ScopesHashModel;
  import org.apache.struts2.views.freemarker.StrutsBeanWrapper;
  import org.apache.struts2.views.freemarker.StrutsClassTemplateLoader;
  import org.omg.CORBA.PUBLIC_MEMBER;
  
  import com.opensymphony.xwork2.ActionInvocation;
  import com.opensymphony.xwork2.util.FileManager;
  import com.opensymphony.xwork2.util.ValueStack;
  
  public class MyTemplateExceptionHandler extends org.apache.struts2.views.freemarker.FreemarkerManager {
  
      freemarker.template.Configuration configuration = new freemarker.template.Configuration();
  
      public MyTemplateExceptionHandler() {
          System.out.println("MyTemplateExceptionHandler constructor()");
          configuration.setTemplateExceptionHandler(new Test1());
      }
  
      class Test1 implements TemplateExceptionHandler {
  
          @Override
          public void handleTemplateException(TemplateException te, Environment env, java.io.Writer out) throws TemplateException {
              System.out.println("MyTemplateExceptionHandler1 handleTemplateException()");
              try {
                  out.write("[ERROR TEST TEST: " + te.getMessage() + "]");
              } catch (IOException e) {
                  throw new TemplateException("Failed to print error message. Cause: " + e, env);
              }
          }
      }
  }
  </code></pre>
  
  <p>My code is going into MyTemplateExceptionHandler constructor(). But not into MyTemplateExceptionHandler1 handleTemplateException(). What do I need to do?</p>
  
  <p>I am still seeing the yellow FTL stack trace.</p>
  
  <p>Same thing is being pointed out on this blog: <a href="http://blog.cherouvim.com/freemarker-exception-handling/" rel="noreferrer">http://blog.cherouvim.com/freemarker-exception-handling/</a> Where excatly do I configure my freemarker and how? I am still stuck as to where that line needs to go.</p>
  
  <p>My other question is, the class posted on the blog seems to be an inner class, do I just put that inner class into any class or is that an outer class?</p>