如何处理C中Tcl_EvalFile()返回的TCL_ERROR?

时间:2019-04-08 17:21:34

标签: c tcl

我有一个C程序,该程序通过Tcl_EvalFile()调用TCL解释器。我正在检查Tcl_EvalFile的状态返回,并且知道它何时产生与TCL_OK不同的东西。但是,我没有像使用tclsh那样在程序中得到任何追溯报告。

我知道将C函数嵌入TCL的情况,但在我的情况下不起作用。我实际上是从Lua程序正在调用的C函数调用TCL。所示示例代码是简化版本。

这是对Tcl_EvalFile()的调用:

  if ((status = Tcl_EvalFile(interp, script)) != TCL_OK)
    {
      /* I would like to handle the error here before Tcl_Exit()*/
      Tcl_Exit(status);
      return TCL_ERROR;
    }

是否可以调用一个TCL函数,该函数将产生与tclsh相似的回溯消息?

1 个答案:

答案 0 :(得分:1)

处理错误最重要的一件事情就是打印错误消息!第二个最重要的事情是打印堆栈跟踪。 Tcl将错误消息放入解释器结果中,并将堆栈跟踪放入特殊的全局变量errorInfo中。

if ((status = Tcl_EvalFile(interp, script)) != TCL_OK) {
    // Get the error message out of the way *right now*
    fprintf(stderr, "ERROR when reading file %s: %s\n", script,
            Tcl_GetStringResult(interp));
    // Most convenient access for this is to run a tiny Tcl script.
    Tcl_Eval(interp, "puts {STACK TRACE:}; puts $errorInfo; flush stdout;");
    Tcl_Exit(status);
    return TCL_ERROR; // This should be unreachable; Tcl_Exit doesn't return...
}