local function div(a,b)
if b==0 then error("除数不能为0") end
return a/b
end
local ok, res = pcall(div, 10, 0)
if ok then
print("结果:", res)
else
print("错误:", res) -- 错误:除数不能为0
end
2. xpcall(带错误处理函数)
local function errHandler(err)
return "捕获错误:"..err
end
local ok, res = xpcall(div, errHandler, 10, 0)
print(res) -- 捕获错误:除数不能为0