当前位置:   article > 正文

lua 错误处理_lua调用失败

lua调用失败

lua 错误处理

      

          

                              

语法错误

         

语法错误编译不能通过,通常是由程序使用不当引起

       

示例:nil与整数相加

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. > a==2
  3. false
  4. -- a由于未赋值,默认为nil,不能和数值相加
  5. > a+1
  6. stdin:1: attempt to perform arithmetic on a nil value (global 'a')
  7. stack traceback:
  8. stdin:1: in main chunk
  9. [C]: in ?

      

示例:语法缺少关键词

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. > t={1,2,3,4,5,6}
  3. -- 遍历表:关键词是in
  4. > for key,value int pairs(t) print(key,value) end
  5. stdin:1: 'in' expected near 'int'
  6. -- 遍历表,缺少关键词do
  7. > for key,value in pairs(t) print(key,value) end
  8. stdin:1: 'do' expected near 'print'
  9. -- 遍历表,正常输出
  10. > for key,value in pairs(t) do print(key,value) end
  11. 1 1
  12. 2 2
  13. 3 3
  14. 4 4
  15. 5 5
  16. 6 6

             

                  

                              

运行错误

         

运行错误可通过编译,在运行时由于参数传递等原因报错

       

示例:函数参数不对导致运行报错

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. > function fun(a,b) print(a+b) end
  3. -- 传入1个参数,另一个参数没有赋值,默认为nil,nil不能与整数相加
  4. > fun(1)
  5. stdin:1: attempt to perform arithmetic on a nil value (local 'b')
  6. stack traceback:
  7. stdin:1: in function 'fun'
  8. (...tail calls...)
  9. [C]: in ?
  10. -- 传入两个数值,可正常相加
  11. > fun(1,2)
  12. 3
  13. -- 字符串类型不能相加
  14. > fun('a','b')
  15. stdin:1: attempt to add a 'string' with a 'string'
  16. stack traceback:
  17. [C]: in metamethod 'add'
  18. stdin:1: in function 'fun'
  19. (...tail calls...)
  20. [C]: in ?

       

              

                              

assert 函数

         

assert(v, message):当v为false时,输出报错信息message

  1. Raises an error if the value of its argument v is false (i.e., nil or false);
  2. otherwise, returns all its arguments. In case of error, message is the error
  3. object; when absent, it defaults to "assertion failed!"
  4. * 当参数v是false时,输出错误信息,如果缺省message,默认输出"assertion failed!"
  5. * 当参数v是true时,返回所有参数(v message)

       

示例

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. -- assert(false)
  3. > assert(false)
  4. stdin:1: assertion failed!
  5. stack traceback:
  6. [C]: in function 'assert'
  7. stdin:1: in main chunk
  8. [C]: in ?
  9. -- assert(nil)
  10. > assert(nil)
  11. stdin:1: assertion failed!
  12. stack traceback:
  13. [C]: in function 'assert'
  14. stdin:1: in main chunk
  15. [C]: in ?
  16. -- assert("nil")
  17. > assert("nil")
  18. nil
  19. -- assert(2>1)
  20. > assert(2>1)
  21. true
  22. -- assert(2<1,"2不小于1")
  23. > assert(2<1,"2不小于1")
  24. stdin:1: 2不小于1
  25. stack traceback:
  26. [C]: in function 'assert'
  27. stdin:1: in main chunk
  28. [C]: in ?
  29. -- 输出v、message
  30. > assert(1,"这是错误信息")
  31. 1 这是错误信息
  32. > assert(0,"0不是false")
  33. 0 0不是false

         

              

                              

error 函数

         

error(message, level):输出错误信息,该函数不会返回

  1. Raises an error (see §2.3) with message as the error object.
  2. This function never returns.
  3. * 中止正在执行的程序,输出错误信息message,该函数不会返回
  4. Usually, error adds some information about the error position
  5. at the beginning of the message, if the message is a string.
  6. * 通常,error函数会附加一些错误位置的信息到message头部
  7. The level argument specifies how to get the error position.
  8. With level 1 (the default), the error position is where the
  9. error function was called.
  10. * level=1(默认):为调用error位置(文件+行号)
  11. Level 2 points the error to where the function that called
  12. error was called; and so on.
  13. * level=2:指出哪个调用error的函数的函数
  14. Passing a level 0 avoids the addition of error position
  15. information to the message.
  16. * level=0:不添加错误位置信息

        

示例:level=0

  1. huli@hudeMacBook-Pro ~ % cat error.lua
  2. function fun(a,b)
  3. error("出错了",0)
  4. print(a+b)
  5. end
  6. fun(1+2)
  7. -- 不添加错误位置信息
  8. huli@hudeMacBook-Pro ~ % lua error.lua
  9. lua: 出错了
  10. stack traceback:
  11. [C]: in function 'error'
  12. error.lua:2: in function 'fun'
  13. error.lua:6: in main chunk
  14. [C]: in ?

     

示例:level=1(默认)

  1. -- level=1
  2. huli@hudeMacBook-Pro ~ % cat error2.lua
  3. function fun2(a,b)
  4. error("出错了",1)
  5. print(a+b)
  6. end
  7. fun2(2+2)
  8. -- 错误信息:前面添加文件(error2.lua)、error函数所在行号(2)
  9. huli@hudeMacBook-Pro ~ % lua error2.lua
  10. lua: error2.lua:2: 出错了
  11. stack traceback:
  12. [C]: in function 'error'
  13. error2.lua:2: in function 'fun2'
  14. error2.lua:6: in main chunk
  15. [C]: in ?

       

示例:level=2

  1. -- level=2
  2. huli@hudeMacBook-Pro ~ % cat error3.lua
  3. function fun3(a,b)
  4. error("出错了",2)
  5. print(a+b)
  6. end
  7. fun3(3+2)
  8. -- 错误信息:文件名(error3.lua)、主函数调用了包含错误函数的函数所在行号(6)
  9. huli@hudeMacBook-Pro ~ % lua error3.lua
  10. lua: error3.lua:6: 出错了
  11. stack traceback:
  12. [C]: in function 'error'
  13. error3.lua:2: in function 'fun3'
  14. error3.lua:6: in main chunk
  15. [C]: in ?

       

               

                              

pcall 函数

         

pcall(function_name, arg, arg2, ...):以保护模式调用函数

  1. Calls the function f with the given arguments in protected mode.
  2. * 用给定的参数,以保护模式调用函数
  3. This means that any error inside f is not propagated; instead,
  4. pcall catches the error and returns a status code.
  5. * 函数的错误信息不会传播,会被pall捕获,然后返回一个状态码(boolean值)
  6. Its first result is the status code (a boolean), which is true
  7. if the call succeeds without errors. In such case, pcall also
  8. returns all results from the call, after this first result.
  9. * 如果调用成功,状态码返回true
  10. * 在true之后,pcall返回调用函数的所有结果
  11. In case of any error, pcall returns false plus the error object.
  12. Note that errors caught by pcall do not call a message handler
  13. * 如果调用失败,pcall返回false,输出错误信息

          

示例:函数没有返回结果

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. > function fun(a,b) print(a+b) end
  3. -- 调用的韩素运行出错,输出状态码false,然后输出错误信息
  4. > pcall(fun,1)
  5. false stdin:1: attempt to perform arithmetic on a nil value (local 'b')
  6. -- 执行函数,返回状态码,
  7. -- 如果函数有返回结果,输出返回结果
  8. > pcall(fun,1,2)
  9. 3
  10. true

       

示例:函数有返回结果

  1. Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
  2. > function fun2(a,b) print(a+b) return a+b end
  3. -- pcall(fun(1)):函数执行报错,随后pcall报错
  4. > pcall(fun2(1))
  5. stdin:1: attempt to perform arithmetic on a nil value (local 'b')
  6. stack traceback:
  7. stdin:1: in function 'fun2'
  8. stdin:1: in main chunk
  9. [C]: in ?
  10. -- pcall(fun2,1):函数执行报错
  11. > pcall(fun2,1)
  12. false stdin:1: attempt to perform arithmetic on a nil value (local 'b')
  13. -- pcall(fun2,1,2):函数正常执行,返回状态码、调用函数的返回结果
  14. > pcall(fun2,1,2)
  15. 3
  16. true 3
  17. -- pcall(fun2(1,3),1,2):先执行函数,函数返回结果4 ==> pcall(4,1,2) pcall调用报错
  18. > pcall(fun2(1,3),1,2)
  19. 4
  20. false attempt to call a number value
  21. -- pcall(fun(1,3)):先执行函数,函数返回结果4 == pcall(4),pcall
  22. -- pcall只能调用函数,不能调用数字
  23. > pcall(fun2(1,3))
  24. 4
  25. false attempt to call a number value

       

        

                              

xpcall 函数

         

xpcall(function_name, messageHandler, arg, arg2, ...):与pcall类似,多了messageHandler(错误处理函数)

  1. This function is similar to pcall, except that
  2. it sets a new message handler msgh.

        

示例:自定义错误处理函数

  1. huli@hudeMacBook-Pro ~ % cat xpcall.lua
  2. --被调用的函数
  3. function fun(a,b)
  4. print(a+b)
  5. end
  6. -- 自定义错误处理函数
  7. function errorHandler(error)
  8. print("error",error)
  9. end
  10. -- 调用函数
  11. xpcall(fun, errorHandler, 1)
  12. -- 执行脚本
  13. huli@hudeMacBook-Pro ~ % lua xpcall.lua
  14. error xpcall.lua:2: attempt to perform arithmetic on a nil value (local 'b')

         

示例:使用debug输出错误调用栈

  1. huli@hudeMacBook-Pro ~ % cat xpcall2.lua
  2. -- 调用的函数
  3. function fun2(a,b)
  4. print(a+b)
  5. end
  6. -- 错误处理函数
  7. function errorHandler()
  8. print(debug.traceback())
  9. end
  10. -- xpcall调用函数
  11. xpcall(fun2, errorHandler, 1)
  12. -- 执行脚本
  13. huli@hudeMacBook-Pro ~ % lua xpcall2.lua
  14. stack traceback:
  15. xpcall2.lua:6: in function 'errorHandler'
  16. xpcall2.lua:2: in function 'fun2'
  17. [C]: in function 'xpcall'
  18. xpcall2.lua:9: in main chunk
  19. [C]: in ?

         

         

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/859067
推荐阅读
相关标签
  

闽ICP备14008679号