从一个函数中抛出Lua错误并由调用该函数的脚本处理,这是否可能?
例如,以下代码将在注释处引发错误:
local function aSimpleFunction(...)
string.format(...) -- Error is indicated to be here
end
aSimpleFunction("An example function: %i",nil)
但是我更愿意捕获错误并通过函数调用者抛出自定义错误
local function aSimpleFunction(...)
if pcall(function(...)
string.format(...)
end) == false then
-- I want to throw a custom error to whatever is making the call to this function
end
end
aSimpleFunction("An example function: %i",nil) -- Want the error to start unwinding here
我的意图是在实际使用中,我的函数会更加复杂,我希望能够提供更有意义的错误信息。
aSimpleFunction()
内部抛出,我已经知道错误会抛出一个错误,但堆栈跟踪不是来自调用者。 - Puddler