17

Is it possible to throw a Lua error from a function to be handled by the script calling the function?

For example the following will throw an error at indicated comment

local function aSimpleFunction(...)
    string.format(...) -- Error is indicated to be here
end

aSimpleFunction("An example function: %i",nil)

But what I would rather do is catch the error and throw out a custom error by the function caller

local function aSimpleFunction(...)
    if pcall(function(...)
        string.format(...)
    end) == false then
       -- I want to throw a custom error to whatever is making the call to this function
    end

end

aSimpleFunction("An example function: %i",nil) -- Want the error to start unwinding here 

The intention being that in my actual use cases my functions would be more complicated and I would like to provide more meaningful error messages

6
  • 2
  • @TomBlodget, make it an answer? ;) Mar 2, 2016 at 0:30
  • @PaulKulchenko - It seems that the idea of writing comments instead of answers is quite infectious ;-) Mar 2, 2016 at 7:53
  • @EgorSkriptunoff, I noticed ;) Mar 2, 2016 at 15:37
  • @PaulKulchenko I'd rather see this question deleted than to write an answer. But, I did want to help the asker learn that intelligible documentation (which the Lua reference manual is) is a primary reference. I don't think that this question and any direct answers to it are helpful to others. If the question was broader then they would be. Mar 2, 2016 at 16:55

3 Answers 3

18

The stack level of an error can be specified when throwing a new error

error("Error Message") -- Throws at the current stack
error("Error Message",2) -- Throws to the caller
error("Error Message",3) -- Throws to the caller after that

Usually, error adds some information about the error position at the beginning of the message. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points 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.

Using the example given in the question

local function aSimpleFunction(...)
    if pcall(function(...)
        string.format(...)
    end) == false then
       error("Function cannot format text",2)
    end

end

aSimpleFunction("An example function: %i",nil) --Error appears here 
9

Use the error function.

error("something went wrong!")
2

Catching an error is as simple as using pcall

My_Error()
    --Error Somehow
end

local success,err = pcall(My_Error)

if not success then
    error(err)
end

Undoubtedly you're asking how this works. Well pcall runs a function in a protected thread (protected-call) and returns a bool, if it ran successfully, and a value (what it returned/the error).

Also don't think this means arguments to the function are impossible, simply pass them to pcall as well:

My_Error(x)
    print(x)
    --Error Somehow
end

local success,err = pcall(My_Error, "hi")

if not success then
    error(err)
end

For more error handling control, see http://www.lua.org/manual/5.3/manual.html#2.3 and http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#xpcall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.