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.

目录
相关文章
mod_lua.cpp:37:10: fatal error: lua.h
mod_lua.cpp:37:10: fatal error: lua.h
85 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
75 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
177 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.
2055 0
|
5月前
|
存储 NoSQL Redis
Redis的Lua脚本有什么作用?
Redis Lua脚本用于减少网络开销、实现原子操作及扩展指令集。它能合并操作降低网络延迟,保证原子性,替代不支持回滚的事务。通过脚本,代码复用率提高,且可自定义指令,如实现分布式锁,增强Redis功能和灵活性。
185 1
|
4月前
|
消息中间件 NoSQL Java
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
206 0
|
1月前
|
存储 JSON Ubuntu
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
|
2月前
|
存储 NoSQL Redis
Tair的发展问题之在Redis集群模式下,Lua脚本操作key面临什么问题,如何解决
Tair的发展问题之在Redis集群模式下,Lua脚本操作key面临什么问题,如何解决
|
4月前
|
JSON 监控 数据格式
使用Lua代码扩展上网行为管理软件的脚本功能
本文介绍了如何使用Lua脚本增强上网行为管理,包括过滤URL、记录用户访问日志、控制带宽和自动提交监控数据到网站。Lua是一种轻量级语言,适合编写扩展脚本。文中提供多个示例代码,如URL过滤器、用户活动日志记录器和带宽控制器,帮助用户根据需求定制网络管理功能。通过这些示例,用户可以快速掌握Lua在上网行为管理中的应用。
151 4