我正在使用 AngelScript,我似乎无法理解的一件事是如何捕获从 C++ 抛出但从 AngelScript 调用的异常。这是我到目前为止所得到的:
// test.as
void main()
{
print("Calling throwSomething...");
throwSomething();
print("Call finished");
}
Run Code Online (Sandbox Code Playgroud)
void print(string)和void throwSomething()是注册到引擎的两个函数,来源如下。根据AngelScript 文档:
向脚本引擎注册的应用程序函数和类方法允许抛出 C++ 异常。虚拟机将自动捕获任何 C++ 异常、中止脚本执行并将控制权返回给应用程序。
以下是提供的用于处理异常的示例代码:
asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(engine->GetModule("test")->GetFunctionByName("func"));
int r = ctx->Execute();
if( r == asEXECUTION_EXCEPTION )
{
string err = ctx->GetExceptionString();
if( err == "Caught an exception from the application" )
{
// An application function threw an exception while being invoked from the script
...
}
}
Run Code Online (Sandbox Code Playgroud)
我几乎逐字地将这段代码复制到我的编辑器中并尝试运行它。不幸的是,即使我将调用包装Execute …