大粨兔奶糖

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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)
posted on 2017-04-21 13:28  大粨兔奶糖  阅读(513)  评论(0编辑  收藏  举报