lua 错误处理
错误处理基本函数
assert 断言
示例程序
local function test(a) assert(type(a) == "number", "a 不是数字") print("hello world") end test(1) test("abcd") test(2)
error
示例程序
local function test() error("这是一个错误测试") end test()
lua 中的 try … catch ...
pcall
示例程序
local function test() error("error") end if pcall(test) then print(1) else print(2) end
- pcall 返回的错误信息只有错误位置, 销毁了调用栈的部分内容
xpcall
示例程序
local function test() n = n / nil end local function errHandle(err) print("Error:", err) end local status = xpcall(test, errHandle) print(status)