是否有可能從函數中拋出一個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 - 似乎寫評論而不是答案的想法是相當具有傳染性的;-) –