捕获/处理脚本函数中抛出的异常 我正在和安吉斯剧本一起玩,有一件事我无法理解,就是如何捕捉来自C++的异常,但是从AngelScript调用。以下是到目前为止我得到的信息: // test.as void main() { print("Calling throwSomething..."); throwSomething(); print("Call finished"); }

捕获/处理脚本函数中抛出的异常 我正在和安吉斯剧本一起玩,有一件事我无法理解,就是如何捕捉来自C++的异常,但是从AngelScript调用。以下是到目前为止我得到的信息: // test.as void main() { print("Calling throwSomething..."); throwSomething(); print("Call finished"); },c++,exception,angelscript,C++,Exception,Angelscript,void print(string)和void throwSomething()是注册到引擎的两个函数,源代码如下。根据: 注册到脚本引擎的应用程序函数和类方法允许抛出C++异常。虚拟机将自动捕获任何C++异常,中止脚本执行,并将控制返回给应用程序。p> 下面是为处理异常提供的示例代码: asIScriptContext *ctx = engine->CreateContext(); ctx->Prepare(engine->GetModule("test")->GetF

void print(string)
void throwSomething()
是注册到引擎的两个函数,源代码如下。根据:

注册到脚本引擎的应用程序函数和类方法允许抛出C++异常。虚拟机将自动捕获任何C++异常,中止脚本执行,并将控制返回给应用程序。p> 下面是为处理异常提供的示例代码:

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
    ...
  }
}
我几乎一字不差地将这段代码复制到我的编辑器中,并尝试运行它。不幸的是,即使我将对
Execute
的调用包装在一个try-catch块中,我仍然得到以下输出:

(AngelScript) Calling throwSomething...
(C++) throwSomething Entered...
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Assert(1 == 0) failed, line 68
Abort trap: 6
为了完整起见,下面是通过某些东西和打印的代码:

void throwSomething()
{
    cout << "(C++) throwSomething Entered...\n";
    Assert(1 == 0); // will throw an exception
    cout << "(C++) throwSomething Exiting...\n";
}

void print(string s)
{
    cout << "(AngelScript) " << s << "\n";
}
void throwSomething()
{

CUT你已经检查过下面的链接了吗?没有太多的东西,在天使代码C++代码中,它使用Test/catch(…)来捕获任何异常。我们不知道你使用的编译器和编译选项,很难帮助你。否则几乎相同。