是否有可能从函数中抛出一个Lua错误,由调用函数的脚本来处理?如何抛出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
的意图是在我的实际使用情况下,我的功能会更加复杂,我想提供更有意义的错误消息
的例子[Lua代码可以显式地通过调用误差函数产生一个错误。](http://www.lua.org/manual/5.3/manual .html#2.3) –
@TomBlodget,让它成为答案? ;) –
@PaulKulchenko - 似乎写评论而不是答案的想法是相当具有传染性的;-) –