你如何抛出 Lua 错误?
How do you throw Lua error up?
是否可以从调用该函数的脚本处理的函数中抛出 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
我的意图是在我的实际用例中我的功能会更复杂,我想提供更有意义的错误消息
使用 error
function.
error("something went wrong!")
捕获错误就像使用 pcall
一样简单
My_Error()
--Error Somehow
end
local success,err = pcall(My_Error)
if not success then
error(err)
end
毫无疑问,您是在问这是如何工作的。那么 pcall
在 受保护的线程 (受保护的调用)和 returns 中运行一个函数,如果它 运行 成功,以及一个值(这是什么 returned/the 错误)。
也不要认为这意味着函数的参数是不可能的,只需将它们也传递给 pcall
:
My_Error(x)
print(x)
--Error Somehow
end
local success,err = pcall(My_Error, "hi")
if not success then
error(err)
end
有关更多错误处理控件,请参阅http://www.lua.org/manual/5.3/manual.html#2.3 and http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#xpcall
抛出新错误时可以指定错误的堆栈级别
error("Error Message") -- Throws at the current stack
error("Error Message",2) -- Throws to the caller
error("Error Message",3) -- Throws to the caller after that
Usually, error adds some information about the error position at the beginning of the message. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing a level 0 avoids the addition of error position information to the message.
使用问题中给出的例子
local function aSimpleFunction(...)
if pcall(function(...)
string.format(...)
end) == false then
error("Function cannot format text",2)
end
end
aSimpleFunction("An example function: %i",nil) --Error appears here
是否可以从调用该函数的脚本处理的函数中抛出 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
我的意图是在我的实际用例中我的功能会更复杂,我想提供更有意义的错误消息
使用 error
function.
error("something went wrong!")
捕获错误就像使用 pcall
My_Error()
--Error Somehow
end
local success,err = pcall(My_Error)
if not success then
error(err)
end
毫无疑问,您是在问这是如何工作的。那么 pcall
在 受保护的线程 (受保护的调用)和 returns 中运行一个函数,如果它 运行 成功,以及一个值(这是什么 returned/the 错误)。
也不要认为这意味着函数的参数是不可能的,只需将它们也传递给 pcall
:
My_Error(x)
print(x)
--Error Somehow
end
local success,err = pcall(My_Error, "hi")
if not success then
error(err)
end
有关更多错误处理控件,请参阅http://www.lua.org/manual/5.3/manual.html#2.3 and http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#xpcall
抛出新错误时可以指定错误的堆栈级别
error("Error Message") -- Throws at the current stack
error("Error Message",2) -- Throws to the caller
error("Error Message",3) -- Throws to the caller after that
Usually, error adds some information about the error position at the beginning of the message. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing a level 0 avoids the addition of error position information to the message.
使用问题中给出的例子
local function aSimpleFunction(...)
if pcall(function(...)
string.format(...)
end) == false then
error("Function cannot format text",2)
end
end
aSimpleFunction("An example function: %i",nil) --Error appears here