handling With a message in Lua

Find this useful? Support us: Star on GitHub 6
Category: Error handling | Language: Lua

In Lua, the try/catch mechanism does not exist by default, but it can be implemented using pcall. pcall is a Lua function that calls a function and catches any errors that occur during the execution of that function. This can be used to implement try/catch/finally behavior.

Here is an example of how to use pcall to implement a try/catch/finally block:

-- Define a function that may throw an error:
function divide(a, b)
  if b == 0 then
    error("Division by zero!")
  end
  return a / b
end

-- Use pcall to catch any errors that the 'divide' function may throw:
local success, result = pcall(divide, 10, 0)

-- Check if the call succeeded:
if success then
print("The result is:", result)
else
print("An error occurred:", result)
end

In the example above, the call to pcall attempts to call the divide function with the arguments 10 and 0. Since this would result in a division by zero error, pcall catches the error and returns false, along with the error message as its second return value.

If the call to pcall succeeds, then success will be true and the result of the function call will be stored in result. If an error occurs, then success will be false and the error message will be stored in result.

The finally block can be a single finally statement at the end of the try-catch statement. For example:

try
  -- Code that might throw an error
catch err
  -- Code to handle the error
finally
  -- Code that must be executed no matter what
end

In Lua, the finally block can be implemented using the finally function from the lunadry library, like this:

local finally = require("lunadry").finally

-- Define a function that may throw an error:
function open_file()
local file = io.open("file.txt", "r")
if file == nil then
error("Could not open file 'file.txt' for reading!")
end
return file
end

-- Use pcall to catch any errors that the 'open_file' function may throw:
local success, result = pcall(open_file)

-- Use 'finally' to ensure that the file is closed:
finally(function()
if type(result) == "userdata" then
result:close()
end
end)

-- Check if the call succeeded:
if success then
print("File opened successfully!")
else
print("An error occurred:", result)
end

In the example above, the call to pcall attempts to open a file named file.txt for reading. If the file cannot be opened, then open_file throws an error and pcall returns false and the error message.

If the call to pcall succeeds, then success will be true and the file object will be stored in result. The finally block uses lunadry.finally to ensure that the file is closed no matter what happens. Finally, the code checks whether or not the call succeeded and prints the appropriate message.