确保执行(finally)

异常处理

local function throw_if_true(param)
  local ok = pcall(function()
    if param then
      error("test exception", 0)
    end
  end)
  if not ok then
    print("except")
  end
  -- 没有 finally:这段代码紧跟在 pcall 之后,
  -- 所以无论有没有错误都会运行
  print("finally")
end

throw_if_true(true)
-- except
-- finally
throw_if_true(false)
-- finally