pcall

If you need to handle errors in Lua, you must use the pcall (protected call) function to encapsulate your code.

The pcall function calls its first argument in protected mode, so that it catches any errors while the function is running. If there are no errors, pcall returns true, plus any values returned by the call. Otherwise, it returns false, plus the error message.

local status, err = pcall(function()
    error({code=121})
end)
print(err.code) --> 121
發佈了3 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章