Lua Error Handling
In this article, we will discuss Lua error handling and discuss various techniques for handling errors in Lua.
In programming, error handling allows you to catch and handle errors that may occur during program execution.
Errors can be divided into two types: Syntax errors and run time errors.
Lua Syntax errors
Syntax errors occur when the programmer’s code violates language syntax rules.
Interpreter or compiler detect errors during code compilation.
Syntax errors prevent code from running and are usually easy to fix.
An example of a syntax error is given below:
There is a syntax error in above code. The reason is that the closing parenthesis “)” is missing at the end of the statement, which causes the code to be incomplete and invalid.
Here is another example of a syntax error:
In this code, the function sayHello takes a parameter name and tries to print a greeting with the name included. However, there is a syntax error in the print statement where the string and variable are not properly concatenated with a comma or concatenation operator.
Syntax errors can be prevented by double-checking the code’s syntax and using proper coding conventions. It is good practice to use an integrated development environment (IDE) that checks for syntax errors in real-time.
When we encounter a syntax error, we need to go back to the code, find the error, and fix it. Once we fix the error, we can run the code again to make sure it runs without any syntax errors.
Lua Run Time Errors
Runtime errors occur when the program is executed and something unexpected happens that prevents it from continuing normally.
For example, the following code snippet will result in a runtime error:
The above code results in an error because the variable x is a string and cannot be added to the variable y, which is a number.
Here is another example of a run time error:
This is a runtime error caused by the absence of a required variable.
In this case, the “y” parameter was not passed, resulting in its value being nil, which caused the error.
Assert and Error Functions
The two functions assert and error are often used to handle errors.
The assert function in Lua is used for error handling and to check whether a given condition is true.
When the condition is true, the function does nothing and returns its value. However, if the condition is false, the assert function throws an error with a specified error message.
The syntax for the assert function is as follows:
Let’s look at an example that implements the assert function on the run time error example above:
The code then calls the remainder function with only one argument, passing the value 10. Since the function expects two arguments, the second argument (y) is assumed to be nil. As a result, the second assert function throws an error message “y is not a number”.
The error function in Lua raises an error in the current function or a called function.
The function takes a string argument that specifies the error message to be displayed.
When an error occurs, the error function terminates the function’s execution.
Here’s the syntax for the error function:
error(message [, level])
The message argument is required and should be a string that describes the error.
The level argument is optional and should be an integer that indicates the level at which to report the error. A value of 1 (the default) indicates the current function, 2 indicates the caller of the current function, and so on.
Here is an example of using the error function:
In above example, the remainder function checks if the second argument y is zero. If y is zero, then the function raises an error using the error function with the error message “Division by zero is not allowed“. If y is not zero, then the function returns the result of the modulus operation.
In Lua, error handling is accomplished using the pcall() and xpcall() functions.
The pcall() function is a Lua standard library function that allows you to call a function and catch any errors that might occur during execution.
The pcall() function takes a function and its arguments as input and returns a boolean value indicating whether the function executed successfully or not.
- If the function executed successfully, pcall() returns true and any values that the function returned.
- If the function encounters an error, pcall() returns false and an error message.
Following code snippet illustrates the use of pcall() function in Lua:
The xpcall() function is a more advanced version of pcall().
xpcall() allows you to specify an error handling function that gets called when an error occurs.
The error handling function receives the error message as an argument.
Below example demonstrates the usage of xpcall() function in Lua: