English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Lua Error Handling

程序运行中错误处理是必要的,在我们进行文件操作,数据转移及web service 调用过程中都会出现不可预期的错误。如果不注重错误信息的处理,就会造成信息泄露,程序无法运行等情况。

Error handling during program execution is necessary. In the process of file operations, data transfer, and web service calls, unexpected errors will occur. If we do not pay attention to the handling of error information, it will cause information leakage, program failure, and other situations.

  • Syntax error

  • Running error

Syntax error

Syntax errors are usually caused by improper use of program components (such as operators, expressions). A simple example is as follows:

-- test.lua file
a == 2

The result of the above code execution is:

lua: test.lua:2: syntax error near '=='

As you can see, there is a syntax error above. There is a difference between one '=' and two '='. One '=' is an assignment expression, and two '=' are comparison operations.

Another example:

for a= 1,10
   print(a)
end

The following error will occur when the above program is executed:

lua: test2.lua:2: 'do' expected near 'print'

Syntax errors are simpler than runtime errors. Runtime errors cannot locate the specific error, while syntax errors can be resolved quickly, as in the above example, we just need to add 'do' under the for statement:

for a= 1,10
do
   print(a)
end

Running error

Running error is that the program can run normally, but will output error information. For example, due to incorrect parameter input, the program will report an error when executed:

function add(a, b)
   return a+b
end
add(10)

When we compile and run the following code, the compilation can be successful, but when running, the following error will occur:

lua: test2.lua:2: attempt to perform arithmetic on local 'b' (a nil value)
stack traceback:
    test2.lua:2: in function 'add'
    test2.lua:5: in main chunk
    [C]: ?

In Lua, when calling a function, it can be successfully called even if the actual parameter list and the formal parameter list are inconsistent. The extra parameters will be discarded, and the missing parameters will be replaced by nil.

The above error message is due to the parameter b being replaced by nil, and nil participated in + operation.

suppose add inside the function is not "return a"+b" but "print(a, b)" if so, the result will become "10 nil" No error will occur.

Error handling

We can use two functions: assert and error to handle errors. For example:

local function add(a, b)
   assert(type(a) == "number", "a is not a number")
   assert(type(b) == "number", "b is not a number")
   return a+b
end
add(10)

The following error will occur when the above program is executed:

lua: test.lua:3: b is not a number
stack traceback:
    [C]: in function 'assert'
    test.lua:3: in local 'add'
    test.lua:6: in main chunk
    [C]: in ?

In the example, assert first checks the first parameter. If there is no problem, assert does nothing; otherwise, assert throws an error with the second parameter as the error message.

error function

Syntax format:

error (message [, level])

Function: Terminate the function being executed and return the content of the message as the error information (the error function will never return)

In most cases, error will attach some error location information to the header of the message.

The Level parameter indicates the error location obtained:

  • Level=1[Default]: For the call error location (file+Line number)

  • Level=2: Indicates which function called error

  • Level=0: Do not add error location information

pcall and xpcall, debug

In Lua, you can use the function pcall (protected call) to wrap the code that needs to be executed to handle errors.

pcall accepts a function and the parameters to be passed to the latter, and executes it. The execution result is: error or no error; return value true or false, errorinfo.

Syntax format as follows

if pcall(function_name, ... ) then
-- No errors
else
-- Some errors
end

Simple example:

> =pcall(function(i) print(i) end, 33)
33
true
   
> =pcall(function(i) print(i) error('error..') end, 33)
33
false stdin:1: error..

Here, pay attention to the logical judgment of the return value:

> function f() return false,2 end
> if f() then print '1'else print '0' end
0

pcall calls the first parameter in a 'protected mode', so pcall can catch any errors during function execution.

When an error occurs, it is often desirable to have more debugging information than just the location of the error. However, when pcall returns, it has already destroyed part of the call stack.

Lua provides the xpcall function, which accepts the second parameter—a error handling function. When an error occurs, Lua will call the error handling function before unwinding the call stack, allowing additional information about the error to be obtained using the debug library.

The debug library provides two general error handling functions:

  • debug.debug: provides a Lua prompt for users to check the cause of the error

  • debug.traceback: builds an extended error message based on the call stack

>=xpcall(function(i) print(i) error('error..') end, function() print(debug.traceback()) end, 33)
33
stack traceback:
stdin:1: in function <stdin:1>
[C]: in function 'error'
stdin:1: in function <stdin:1>
[C]: in function 'xpcall'
stdin:1: in main chunk
[C]: in ?
false nil

xpcall Usage Example 2:

function myfunction()
   n = n/nil
end
function myerrorhandler(err)
   print("ERROR:", err)
end
status = xpcall(myfunction, myerrorhandler)
print(status)

The following error will occur when the above program is executed:

ERROR: test2.lua:2: attempt to perform arithmetic on global 'n' (a nil value)
false