Lua學習筆記——異常處理pcall、xpcall、debug
目錄
1、博客介紹
本篇文章記錄lua學習知識點,異常處理的方法pcall、xpcall、debug
2、內容
(1)pcall
pcall有兩個參數,寫法pcall(parm1,parm2),參數parma1爲需要執行的方法,參數parm2爲參數1方法的參數,pcall(~,~)返回一個布爾值,具體的寫法如下,如果testFunc可以在接受參數後正常執行不出錯,那麼將會執行body1和body2的內容,反之則執行body3的內容。
function testFunc(num)
-- body1
end
if pcall(testFunc,2) then
-- body2
else
-- body3
end
(2)xpcall
pcall的進階版,參數一同樣是需要執行的方法,參數二則是一個錯誤處理函數,可以在這個函數內打印錯誤相關的堆棧信息並返回錯誤信息,則xpcall的兩個返回值分別是狀態和錯誤信息。
function testFunc(num)
error('error..')
end
function dealMsg(msg)
print(msg)
print(debug.traceback())
return msg
end
local statue,mag = xpcall(testFunc,dealMsg)
3、推送
Github:https://github.com/KingSun5
4、結語
若是覺得博主的文章寫的不錯,不妨關注一下博主,點贊一下博文,另博主能力有限,若文中有出現什麼錯誤的地方,歡迎各位評論指摘。
QQ交流羣:806091680(Chinar)
該羣爲CSDN博主Chinar所創,推薦一下!我也在羣裏!