当我的 lua 代码抛出错误时,为什么我不能捕获 luabind::error 异常?

2021-12-30 00:00:00 exception-handling lua c++ c++11 luabind

当你从 c++ 调用一个 LUA 函数并且有一个运行时错误 LuaBind 抛出一个 luabind::error 异常,你可以捕获它然后读取堆栈以查看错误是什么.我的调试器肯定会捕获此异常,但是当我让调试器继续运行时,程序会立即终止,而不是在我的代码中捕获异常.

When you call a LUA function from c++ and there is a runtime error LuaBind throws a luabind::error exception that you can catch and then read the stack to see what the error was. My debugger definitely catches this exception but when I let the debugger continue, instead of the exception being caught in my code the program immediately terminates.

异常在来自析构函数 ~proxy_member_void_caller() 的 LuaBind 包含文件中的call_member.hpp"中抛出.

The exception is thrown in "call_member.hpp" in the LuaBind include files from the destructor ~proxy_member_void_caller().

问题出现在简单的测试代码中.我使用 Xcode 5 和 LuaBind 0.9.1.

The problem occurs with simple test code. I am using Xcode 5 with LuaBind 0.9.1.

推荐答案

原来是在析构函数中抛出异常的坏习惯.C++11 的析构函数是隐式的noexcept(true),所以如果发生异常,程序就会终止.LuaBind 在析构函数中使用异常,因此在我的现代编译器中程序终止.将方法签名编辑为:

It turns out that it is bad practice to throw exceptions in destructors. With C++11 destructors are implicitly noexcept(true), so if an exception occurs the program terminates. LuaBind uses exceptions in destructors, so on my modern compiler the program terminated. Editing the method signature to:

~proxy_member_void_caller() noexcept(false) {}

允许您在 c++11 中捕获来自 LuaBind 的异常.

allows you to catch exceptions from LuaBind in c++11.

相关文章