diff --git a/src/main/java/com/samskivert/mustache/Mustache.java b/src/main/java/com/samskivert/mustache/Mustache.java index 7e05e26..3d9098f 100644 --- a/src/main/java/com/samskivert/mustache/Mustache.java +++ b/src/main/java/com/samskivert/mustache/Mustache.java @@ -273,8 +273,8 @@ public class Mustache /** Handles loading partial templates. */ public interface TemplateLoader { - /** Returns a reader for the template with the supplied name. - * Reader will be closed by callee. + /** Returns a reader for the template with the supplied name. + * Reader will be closed by callee. * @throws Exception if the template could not be loaded for any reason. */ Reader getTemplate (String name) throws Exception; } @@ -741,16 +741,22 @@ public class Mustache // we compile our template lazily to avoid infinie recursion if a template includes // itself (see issue #13) if (_template == null) { + Reader tin = null; try { - Reader t = _comp.loader.getTemplate(_name); - _template = _comp.compile(t); - t.close(); + tin = _comp.loader.getTemplate(_name); + _template = _comp.compile(tin); } catch (Exception e) { if (e instanceof RuntimeException) { throw (RuntimeException)e; } else { throw new MustacheException("Unable to load template: " + _name, e); } + } finally { + if (tin != null) try { + tin.close(); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } } } // we must take care to preserve our context rather than creating a new one, which