Lua error handler: error() and pcall()

简介:
pcall函数可用于调用函数, 如果函数调用成功, 返回true以及被调用函数的结果集.
如果调用失败, 返回false以及错误消息.
结合error可以方便的捕获消息和处理错误.
例如 : 
pcall调用一个匿名函数, 结果存储到status和err全局变量, 错误消息可以是字符串, 也可以是其他数据类型, 例如表类型.
因此比较方便把当时的变量值, 错误消息, 错误代码存储到表中.
> status, err = pcall( function() error({code = 121}) end )
> print(status)
false
> print(err)
table: 0x22033b0
> print(err.code)
121


如果函数被正常调用, 返回正常的结果
> status, err,a,b,c = pcall( function(...) return ... end, 1, 2, 3 )
> print(status,err,a,b,c)
true    1       2       3       nil
> p = function(...) return ... end
> status, err,x,y,z = pcall( p, 1,2,3 )
> print(status,err,x,y,z)
true    1       2       3       nil

注意函数的参数是在pcall的参数中传递过去的, 不是直接使用f(1,2,3). 需要注意了.

error和pcall通常的搭配 : 

   function foo ()
        ...
      if unexpected_condition then error() end
        ...
      print(a[i])    -- potential error: `a' may not be a table
        ...
    end
Then, you call foo with pcall:
    if pcall(foo) then
      -- no errors while running `foo'
      ...
    else
      -- `foo' raised an error: take appropriate actions
      ...
    end


[参考]
1. 

pcall (f [, arg1, ···])

Calls function f with the given arguments in protected mode. This means that any error inside f is not propagated; instead, pcall catches the error and returns a status code. Its first result is the status code (a boolean), which is true if the call succeeds without errors. In such case, pcall also returns all results from the call, after this first result. In case of any error,pcall returns false plus the error message.

2. 

error (message [, level])

Terminates the last protected function called and returns  message  as the error message. Function  error  never returns.

Usually, error adds some information about the error position at the beginning of the message, if the message is a string. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing a level 0 avoids the addition of error position information to the message.

目录
相关文章
BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory
BUG!!!======> 编译安装lua 时 lua.c:67:31: fatal error: readline/readline.h: No such file or directory
234 1
mod_lua.cpp:37:10: fatal error: lua.h
mod_lua.cpp:37:10: fatal error: lua.h
214 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
200 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
262 0
|
Linux C语言
lua.c:80:31: fatal error: readline/readline.h: No such file or directory
make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline"make[2]: Entering directory `/root/lua/lua-5.
2226 0
|
缓存 NoSQL 搜索推荐
【📕分布式锁通关指南 03】通过Lua脚本保证redis操作的原子性
本文介绍了如何通过Lua脚本在Redis中实现分布式锁的原子性操作,避免并发问题。首先讲解了Lua脚本的基本概念及其在Redis中的使用方法,包括通过`eval`指令执行Lua脚本和通过`script load`指令缓存脚本。接着详细展示了如何用Lua脚本实现加锁、解锁及可重入锁的功能,确保同一线程可以多次获取锁而不发生死锁。最后,通过代码示例演示了如何在实际业务中调用这些Lua脚本,确保锁操作的原子性和安全性。
780 6
【📕分布式锁通关指南 03】通过Lua脚本保证redis操作的原子性