Lua Error Handling - Lua (programming language)

What is the use of error handling in Lua?

Need for Error Handling

Error handling in Lua is very important as real-world applications frequently use complex operations which include file operations, database transactions and web service calls.

In any programming, there is always a need for error handling. Errors are of two types which include:

  • Syntax errors
  • Run time errors

Syntax Errors

Syntax errors occur due to inappropriate usage of several program components like operators and expressions. Below is a simple example for syntax error.

As you are aware that there is a difference between the use of a single "equal to" and double "equal to", using one instead of the other will lead to an error. One "equal to" refers to assignment while a double "equal to" refers to comparison. Similarly, there are expressions and functions which have their predefined ways of implementation.

Another example for syntax error is shown below

When you run the above program, below output will be displayed

Syntax errors are very easy to handle than run time errors as Lua interpreter locates the error more clearly than in case of runtime error. From the above error, it is clear that adding a do statement before print statement is needed as per the Lua structure.

Run Time Errors

In case of runtime errors, program will execute successfully, but it results in runtime errors due to mistakes in input or mishandled functions. Below is a simple example to show run time error.

When you build the program, it will build successfully and run. Once it runs, it will show a run time error.

This is a runtime error which occurred due to not passing two variables. b parameter is expected and here it is nil and therefore produces an error.

Assert and Error Functions

To handle errors, we often use two functions − assert and error. Below is a simple example.

When you run the above program, below error output will be displayed.

lua: test2.lua:3: b is not a number

stack traceback:

[C]: in function 'assert'

test2.lua:3: in function 'add'

test2.lua:6: in main chunk

[C]: ?

Error (message [, level]) will terminate the last protected function called and returns message as the error message. This function error never returns. Usually, error adds some information about the error position at the beginning of the message and the level argument describes how to get the error position. With level 1 (the default), error position is where the error function was called and Level 2 will point the error to where the function that called error was called and so on. Passing a level 0 avoids the addition of error position information to the message.

pcall and xpcall

In Lua programming, to avoid throwing these errors and to handle errors, functions pcall or xpcall should be used.

pcall (f, arg1, ...) function will call the requested function in protected mode and if some error occurs in function f, it will not throw an error and it will just return the status of error. Below is a simple example using pcall.

When you run the above program, below output will be displayed.

xpcall (f, err) function calls the requested function and also sets the error handler. Any error inside f will not be propagated; instead, xpcall will catch the error, it will call the err function with the original error object and returns a status code.

Below is a simple example for xpcall.

When you run the above program, below output will be displayed.

As a programmer, it is very essential to make sure that you take care of proper error handling while writing programs. Using error handling, you can ensure that unexpected conditions beyond the boundary conditions should be handled without disturbing the user of the program.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Lua (programming language) Topics