Lua study notes-exception handling pcall, xpcall

                                 Lua study notes-exception handling pcall, xpcall, debug


table of Contents

1. Blog introduction

(1)pcall

(2)xpcall

3. Push

4. Conclusion


1. Blog introduction

This article records the knowledge points of Lua learning, the methods of exception handling pcall, xpcall, debug


2. Content

(1)pcall

pcall has two parameters, written as pcall(parm1,parm2), the parameter parma1 is the method to be executed, the parameter parm2 is the parameter of the parameter 1 method, pcall(~,~) returns a boolean value, the specific writing method is as follows, if testFunc can After accepting the parameters, if there is no error, the contents of body1 and body2 will be executed, otherwise the contents of body3 will be executed.

function testFunc(num)
    -- body1
end

if pcall(testFunc,2) then
    -- body2
else
    -- body3
end

(2)xpcall

In the advanced version of pcall, the first parameter is also the method that needs to be executed, and the second parameter is an error handling function. You can print error-related stack information and return error information in this function, and the two return values ​​of xpcall are respectively It is the status and error messages.

function testFunc(num)
    error('error..')
end

function dealMsg(msg)
    print(msg)
    print(debug.traceback())
    return msg
end

local statue,mag = xpcall(testFunc,dealMsg)

3. Push


Github:https://github.com/KingSun5


4. Conclusion


        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/104956760
Recommended