> 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> 这里可以执行一些东西
[参考]
error (message [, level])
Terminates the last protected function called and returnsmessage
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.
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.
xpcall (f, msgh [, arg1, ···])
This function is similar to pcall
, except that it sets a new message handler msgh
.
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.
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
).