lua pcall error

来源:互联网 发布:陈道明 左小青 知乎 编辑:程序博客网 时间:2024/05/21 17:19
function main()    --抛出异常    throw({retcode = "E001",retmsg = "出错了!!!",})end--捕获异常local ok, result = pcall(main)if not ok then    log(m_uuid,"throw err:"..tostring(result) )endERR = {SUCCESS             = { retcode = "0000", retmsg = "success" }, SYSTEM_ERROR        = { retcode = "E001", retmsg = "system error" },MAC_ERROR           = { retcode = "E002", retmsg = "mac error" }, }function throw(err, ret)    local SYSTEM_ERROR = ERR.SYSTEM_ERROR    if type(err) ~= "table" then        error { retcode = SYSTEM_ERROR.retcode, retmsg = string.format("throw : err is %s", tostring(err)), }    end    if ret and type(ret) ~= "table" then        error { retcode = err.retcode, retmsg = tostring(ret) }    end    ret = ret or err or SYSTEM_ERROR    ret.retcode = (err or SYSTEM_ERROR).retcode    ret.retmsg = ret.retmsg or err.retmsg    error(ret)end
原创粉丝点击