我有这个代码 snnipet (整个程序编译和链接正确):
...
try
{
boost::python::exec_file(
"myscript.py", // this file contains a syntax error
my_main_namespace,
my_local_namespace
);
return true;
}
catch(const boost::python::error_already_set &)
{
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch(&ptype, &pvalue, &ptraceback);
// the next line crashes on syntax error
std::string error = boost::python::extract<std::string>(pvalue);
...
}
程序试图执行的文件有语法错误,所以抛出异常。当程序尝试获取错误消息时崩溃...
该代码可以很好地处理运行时错误,但会因语法错误而崩溃。
我怎样才能得到这种错误的错误字符串?
提前致谢