localfunctionfunc() error("here is my error", 0) end
print('==== error level is 0 ====') func()
当level为0时,执行结果为:
1 2 3 4 5 6 7
==== error level is 0 ==== luajit: here is my error stack traceback: [C]: in function 'error' test.lua:2: in function 'func' test.lua:6: in main chunk [C]: at 0x55f390ecd440
当level为1时,执行结果为:
1 2 3 4 5 6 7
==== error level is1 ==== luajit: test.lua:2: here is my error stack traceback: [C]: in function'error' test.lua:2: in function'func' test.lua:6: in main chunk [C]: at 0x55db23316440
当level为2时,执行结果为:
1 2 3 4 5 6 7
==== error level is2 ==== luajit: test.lua:6: here is my error stack traceback: [C]: in function'error' test.lua:2: in function'func' test.lua:6: in main chunk [C]: at 0x559c71e57440
localfunctionhandler(err) print("ERROR: ", err) print(debug.traceback()) end
localfunctionfunc() n = 1 / 1 end
status = xpcall(func, handler) print(status)
localfunctionfunc() n = 1 / nil end
status = xpcall(func, handler) print(status)
其执行结果为:
1 2 3 4 5 6 7 8 9
true ERROR: test.lua:15: attempt to perform arithmetic on a nil value stack traceback: test.lua:3: in function '__div' test.lua:15: in function <test.lua:14> [C]: in function 'xpcall' test.lua:18: in main chunk [C]: at 0x563ac1f67440 false