Lua error message and traceback

简介:
Lua执行遇到错误时, 如果错误信息输出为字符串类型, 那么还会附加两个内容, 
1. 输出来源, 例如文件或标准输入
2. 错误位置, 行号. 
例如 : 
> function foo(str)
>>   if type(str) ~= 'string' then
>>     print("line 1\n") 
>>     error("string expected")
>>   end
>>   print("end")
>> end
> foo(1)
line 1

stdin:4: string expected  -- 这行是错误信息, 包括错误信息的来源stdin, 以及行号4. foo函数第四行报错.
stack traceback:  -- 这里开始时traceback信息.
        [C]: in function 'error'
        stdin:4: in function 'foo'
        stdin:1: in main chunk
        [C]: in ?


如果把error的第二个参数( 默认是1 )改成2, 那么输出的行号会变成1, 实际触发错误的位置是error函数, 2表示层级. 
> function foo(str)
  if type(str) ~= 'string' then
    print("line 1\n") 
    error("string expected", 2)
  end
  print("end")
end
> foo(1)
line 1

stdin:1: string expected
stack traceback:
        [C]: in function 'error'
        stdin:4: in function 'foo'
        stdin:1: in main chunk
        [C]: in ?


traceback的信息
> do
print("hello")
foo(1)
end
hello
line 1

stdin:4: string expected
stack traceback:
        [C]: in function 'error'
        stdin:4: in function 'foo'
        stdin:3: in main chunk  -- 如果触发错误的函数在main chunk中第三行, 则这里输出第三行.
        [C]: in ?


注意, 使用pcall的话, msg将缺失traceback的信息. 使用xpcall解决.
函数 : 
> function foo(str)
  if type(str) ~= 'string' then
    print("line 1\n") 
    error("string expected")
  end
  print("end")
end
使用pcall调用foo, 传入参数1.
> res, msg = pcall(foo, 1)
line 1

> print(res)
false
> print(msg)  -- msg中没有存储traceback的信息.
stdin:4: string expected

使用xpcall可以解决这个问题, 比pcall多了一个参数, error handler function. 这里使用debug.traceback.
> res, msg = xpcall(foo, debug.traceback, 1)
line 1

> print(msg) -- 使用debug.traceback可以将traceback的信息存储到msg变量.
stdin:4: string expected
stack traceback:
        [C]: in function 'error'
        stdin:4: in function <stdin:1>
        [C]: in function 'xpcall'
        stdin:1: in main chunk
        [C]: in ?
> print(res)
false


如果使用debug.debug作为error handler的话, 那么将进入handler命令行.
> res, msg = xpcall(foo, debug.debug, 1)
line 1

lua_debug> 这里可以执行一些东西


[参考]
1. 

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.

2. 

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.

3. 

xpcall (f, msgh [, arg1, ···])

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

4. 

debug.debug ()

Enters an interactive mode with the user, running each string that the user enters. Using simple commands and other debug facilities, the user can inspect global and local variables, change their values, evaluate expressions, and so on. A line containing only the word cont finishes this function, so that the caller continues its execution.

Note that commands for debug.debug are not lexically nested within any function and so have no direct access to local variables.

5. 

debug.traceback ([thread,] [message [, level]])

If message is present but is neither a string nor nil, this function returns message without further processing. Otherwise, it returns a string with a traceback of the call stack. An optionalmessage string is appended at the beginning of the traceback. An optional level number tells at which level to start the traceback (default is 1, the function calling traceback).

目录
相关文章
|
1月前
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
37 1
mod_lua.cpp:37:10: fatal error: lua.h
mod_lua.cpp:37:10: fatal error: lua.h
93 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
84 0
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
lua.c:82:10: fatal error: readline/readline.h: 没有那个文件或目录
185 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.
2071 0
|
5月前
|
消息中间件 NoSQL Java
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
Redis系列学习文章分享---第六篇(Redis实战篇--Redis分布式锁+实现思路+误删问题+原子性+lua脚本+Redisson功能介绍+可重入锁+WatchDog机制+multiLock)
229 0
下一篇
无影云桌面