我无法捕捉到 STGroupFile 抛出的 STException。这是个问题。如果模板不好,我需要中止。为了重现这个问题,我有一个名为 tmp.stg 的不正确的模板文件:
temp1(param1)::=<<
%if(param1)%
%param1:{%temp2(p)%}; separator"\n"%
%endif%
>>
这个处理它的常规代码:
#!/usr/bin/env groovy
@Grab(group="org.antlr", module="ST4", version="4.0.8")
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.NumberRenderer;
public class Gex {
public static void main(String [] args) {
System.out.println("Processing...")
File fn = new File("tmp.stg")
STGroupFile group;
try {
group = new STGroupFile(fn.toString());
} catch (Throwable e) {
throw new Exception("Caught first exception");
}
try {
group.registerRenderer(Integer.class, new NumberRenderer());
} catch (Throwable e) {
throw new Exception("Caught second exception");
}
throw new Exception("You should not see this");
}
}
Gex.main()
当我运行该脚本时,我收到一条错误消息,但我无法捕获异常:
can't load group file file:tmp.stg
错误消息来自 STGroupFile.java:
throw new STException("can't load group file "+fileName, e);
但我无法捕捉到这个异常。我怎样才能捕捉到这个异常并中止?