Lua Error Handling
Error handling during program execution is necessary. During file operations, data transfers, and web service calls, unexpected errors can occur. If error information is not handled properly, it can lead to information leaks, program failures, and other issues.
Any programming language requires error handling. Error types include:
* Syntax errors
* Runtime errors
* * *
## Syntax Errors
Syntax errors are usually caused by improper use of program components (such as operators and expressions). A simple example is as follows:
-- test.lua file a == 2
The execution result of the above code is:
lua: test.lua:2: syntax error near '=='
As you can see, a syntax error occurred above. There is a difference between one "=" sign and two "=" signs. One "=" is an assignment expression, while two "=" is a comparison operator.
Another example:
## Example
for a=1,10
print(a)
end
Running the above program will produce the following error:
lua: test2.lua:2: 'do' expected near 'print'
Syntax errors are simpler than runtime errors. Runtime errors cannot pinpoint the exact error, while syntax errors can be resolved quickly. For example, in the above instance, we just need to add `do` after the `for` statement:
## Example
for a=1,10
do
print(a)
end
* * *
## Runtime Errors
Runtime errors occur when the program can execute normally but outputs an error message. The following example shows an error during program execution due to incorrect parameter input:
function add(a,b) return a+b end add(10)
When we compile and run the following code, compilation is successful, but the following error occurs during runtime:
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 : ?
In Lua, when calling a function, even if the actual parameter list and the formal parameter list are inconsistent, the call can still succeed. Extra parameters are discarded, and missing parameters are set to `nil`.
The above error message is due to parameter `b` being set to `nil`, and `nil` participating in the `+` operation.
If the **add** function contained **"print(a,b)"** instead of **"return a+b"**, the result would be **"10 nil"** and no error would occur.
* * *
## Error Handling
We can use two functions: `assert` and `error` to handle errors. An example is as follows:
## 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)
Running the above program will produce the following error:
lua: test.lua:3: b is not a number stack traceback:: in function 'assert'test.lua:3: in local 'add'test.lua:6: in main chunk : in ?
In the example, `assert` first checks the first parameter. If there is no problem, `assert` does nothing; otherwise, `assert` throws an error message using the second parameter.
### error function
Syntax format:
error (message [, level])
Functionality: Terminates the currently executing function and returns the content of `message` as the error message (the `error` function never returns).
Usually, `error` appends some error location information to the beginning of the message.
The `level` parameter indicates where to get the error location:
* Level=1 : The location where `error` is called (file + line number)
* Level=2: The function that called the function that called `error`
* Level=0: Does not add error location information
* * *
## pcall, xpcall, and debug
To handle errors in Lua, you can use the function `pcall` (protected call) to wrap the code that needs to be executed.
`pcall` receives a function and the parameters to pass to it, and executes it. The execution result: error or no error; returns `true` or `false`, and error information.
The syntax format is as follows:
if pcall(function_name, β¦.) then-- No errorelse-- Some errorend
Simple example:
## Example
>=pcall(function(i)print(i)end,33)
33
true
>=pcall(function(i)print(i)error('error..')end,33)
33
false stdin:1:error..
> function f() return false,2 end> if f() then print '1' else print '0' end0
`pcall` calls the first parameter in "protected mode," so `pcall` can catch any errors during function execution.
Usually, when an error occurs, we hope to get more debugging information, not just the location where the error occurred. But when `pcall` returns, it has already destroyed part of the call stack.
Lua provides the `xpcall` function. `xpcall` receives a second parameterβan error handling function. When an error occurs, Lua will call the error handling function before unwinding the call stack, so you can use the `debug` library in this function to obtain additional information about the error.
The `debug` library provides two general-purpose error handling functions:
* `debug.debug`: Provides a Lua prompt for users to check the cause of the error
* `debug.traceback`: Constructs 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 : in function 'error' stdin:1: in function : in function 'xpcall' stdin:1: in main chunk : in ?false nil
Example 2 of using `xpcall`:
## Example
function myfunction ()
n = n/nil
end
function myerrorhandler( err )
print("ERROR:", err )
end
status =xpcall( myfunction, myerrorhandler )
print( status)
Running the above program will produce the following error:
ERROR:test2.lua:2: attempt to perform arithmetic on global 'n' (a nil value)false
YouTip