Lua:
Handling errors

How to:

Lua uses two main functions for error handling: pcall and xpcall. Here’s how you use them:

function might_fail()
    if math.random() > 0.5 then
        error("Oops! Something went wrong.")
    else
        print("All good!")
    end
end

-- Using pcall
local success, errorMessage = pcall(might_fail)

if success then
    print("Success!")
else
    print("Caught an error:", errorMessage)
end

-- Using xpcall with an error handler
function myErrorHandler(err)
    print("Error Handler says:", err)
end

local status = xpcall(might_fail, myErrorHandler)
print("Was the call successful?", status)

Sample output could be:

Caught an error: Oops! Something went wrong.
Error Handler says: Oops! Something went wrong.
Was the call successful? false

Or, if no error occurs:

All good!
Success!
All good!
Was the call successful? true

Deep Dive

Handling errors, or “exception handling,” wasn’t always a thing. Early programs crashed – a lot. As coding evolved, so did the need for stability. Lua’s approach is simple compared to some languages. There are no try/catch blocks, just pcall and xpcall. The former protects a function call, returning a status and any error. The latter adds an error handling function, useful for custom clean-up or logging.

An alternative in Lua is to use assert, which can serve a similar purpose by throwing an error if its condition is false. But it’s not as flexible as pcall for complex error handling scenarios.

Internally, pcall and xpcall work by setting up a “protected environment” for the function to run. If an error pops up, the environment catches it and can either handle it right away or pass it back for the program to handle.

See Also