gpt4 book ai didi

c - 如何在 C 中处理从 Tcl_EvalFile() 返回的 TCL_ERROR?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:51 25 4
gpt4 key购买 nike

我有一个 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 产生的回溯消息?

最佳答案

要处理错误,您可以做的最重要的一件事情就是打印出错误消息!第二个最重要的事情是打印堆栈跟踪。 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...
}

关于c - 如何在 C 中处理从 Tcl_EvalFile() 返回的 TCL_ERROR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55578627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com