pcall 指的是 protected call
相似其它语言里的 try-catch
, 使用pcall 调用函数,若是函数 f 中发生了错误, 它并不会抛出一个错误,而是返回错误的状态, 为被执行函数提供一个保护模式,保证程序不会意外终止函数
语法lua
pcall( f , arg1,···)
返回值debug
函数执行状态 (boolean)code
没有错误返回 true
orm
有错误返回 false
input
发生错误返回错误信息,不然返回函数调用返回值string
pcall 示例it
使用pcall 处理错误io
function square(a) return a * "a" end local status, retval = pcall(square,10); print ("Status: ", status) -- 打印 "false" print ("Return Value: ", retval) -- 打印 "input:2: attempt to perform arithmetic on a string value"
正常没错误function
function square(a) return a * a end local status, retval = pcall(square,10); print ("Status: ", status) -- 打印 "true" print ("Return Value: ", retval) -- 打印 "100"
xpcall (f, msgh [, arg1, ···])
xpcall
相似 pcall
xpcall接受两个参数:调用函数、错误处理函数
好比使用 debug.traceback 获取栈信息
> status, err, ret = xpcall(square, debug.traceback, 10) > status false > err stdin:2: attempt to perform arithmetic on a string value stack traceback: stdin:2: in function 'square' [C]: in function 'xpcall' stdin:1: in main chunk [C]: in ? >