无法在StringTemplate 4中捕获STException

时间:2014-05-06 00:47:12

标签: stringtemplate-4

我无法捕获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);

但是我无法捕捉到这个例外。如何捕获此异常并中止?

2 个答案:

答案 0 :(得分:2)

根据ANTLR Guy的建议,我扩展了STErrorListener以抛出异常而不是向stderr打印消息。它看起来像这样:

档案:lib/GexListener.groovy

import org.stringtemplate.v4.STErrorListener;
import org.stringtemplate.v4.misc.STMessage;
import org.stringtemplate.v4.misc.ErrorType;

class GexListener implements STErrorListener {
  @Override
  public void compileTimeError(STMessage msg) {
    throw new Exception(msg.toString());
  }
  @Override
  public void runTimeError(STMessage msg) {
    if ( msg.error != ErrorType.NO_SUCH_PROPERTY ) { // ignore these
      throw new Exception(msg.toString());
    }
  }
  @Override
  public void IOError(STMessage msg) {
    throw new Exception(msg.toString());
  }
  @Override
  public void internalError(STMessage msg) {
    throw new Exception(msg.toString());
  }
  public void error(String s) { error(s, null); }
  public void error(String s, Throwable e) {
    System.err.println(s);
    if ( e!=null ) {
      throw new Exception(msg.toString());
    }
  }
}

然后主脚本bin/gex.groovy看起来像这样:

#!/bin/bash
//usr/bin/env groovy -cp ${0%/*}/../lib "$0" "$@"; exit $?

@Grab(group="org.antlr", module="ST4", version="4.0.8")
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.NumberRenderer;
import GexListener

public class Gex {
  public static void main(String [] args) {
    System.out.println("Processing...")
    File fn = new File("tmp.stg")
    STGroupFile group;
    GexListener listener = new GexListener();
    group = new STGroupFile(fn.toString());
    group.setListener(listener);
    group.registerRenderer(Integer.class, new NumberRenderer());
    System.out.println("You should not see this line")
  }
}

Gex.main()

当它执行时,有一个令人讨厌的副作用,其中stacktrace被打印两次,但程序在打印最后一句“你不应该看到这一行”之前中止,这是所希望的行为。

答案 1 :(得分:0)

正如您在另一封电子邮件中指出的那样:&#34;我发现该异常实际上已被捕获而未被重新抛出。这发生在STGroup.java中:&#34;

catch (Exception e) { errMgr.IOError(null, ErrorType.CANT_LOAD_GROUP_FILE, e, fileName); }

为什么不重写IOError函数(或它调用的侦听器中的函数?)才重新抛出e?