重新抛出异常

异常处理

local function method_with_error(level)
  local ok, err = pcall(function()
    error("test exception", 0)
  end)
  if not ok then
    -- 在此做一些局部处理,
    -- 再把错误继续向外传递
    error(err, level)
  end
end

-- 级别 0:错误原样继续传递
local _, msg = pcall(method_with_error, 0)
-- msg = test exception

-- 默认级别会在文本中添加位置
local _, with_position = pcall(method_with_error)
-- with_position = main.lua:8: test exception

print("msg = " .. msg)
print("with_position = " .. with_position)