Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:29+00:00 2026-05-28T01:31:29+00:00

I have a lua function which takes multiple parameters and returns as many values

  • 0

I have a lua function which takes multiple parameters and returns as many values as there are parameters. Each return value corresponds to a parameter. To illustrate, consider a function which reads the value for a key/value pair from a database:

val1, val2, val3 = my_function("key1", "key2", "key3");
val1 = my_function("key1");

What is the best way to return an error from my_function? (e.g. if a supplied “key” is invalid)

I understand one way is to return two values on error, nil and an error string. Is this the best approach? For example:

val1, val2, val3 = my_function("key1", "key2", "key3");
if val1 then
    -- Use val1, val2, and val3.
else
    print("ERROR: " .. val2);
end

Edit

Some additional points:

  1. The lua script is being executed from within a C program using lua_pcall().
  2. The C program must not abort if the script fails.
  3. my_function() is implemented in C.
  4. When my_function() fails, it should also return an error code (or message) indicating the reason it failed.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:31:29+00:00Added an answer on May 28, 2026 at 1:31 am

    The standard way of throwing errors in Lua is via the error function (manual, api) or via assert (which internally uses error anyway).

    Since your function is in C, you should be calling lua_error inside it, to gain the same effect.

    But keep in mind that your function now is “insecure”. If unmodified, the following code will do the equivalent of “throwing an exception” and thus halting the program, if key1, key2 or key3 are “erroneous”:

    val1, val2, val3 = my_function("key1", "key2", "key3")
    

    Sometimes it’s ok to let the program “just crash” if the inputs are wrong. Depending on your setup, the user will get a message with the last error, and a stack trace, or something along those lines.

    If “letting the program crash” is not an option, you can do the equivalent of surrounding it with a “try-catch” block by using the pcall function, and adding a new variable called ok:

    ok, val1, val2, val3 = pcall(my_function, "key1", "key2", "key3")
    if ok then
      -- Use val1, val2, and val3.
    else
      print("ERROR: " .. val1) -- notice that we use val1 here, not val2
    end
    

    Notice that you don’t have to put pcall exactly on top of my_function. As with exceptions, the error recuperation can happen higher in the call chain: in the function calling my_function, or the function calling that one, etc. For example, if you call my_function from a function called parent, and parent from one called grandParent, you can do this:

    -- parent is "insecure" since my_function is "unprotected"
    function parent(key1, key2)
      local val1, val2, val3 = my_function(key1, key2, "key3")
      return val1, val2, val3
    end
    
    -- grandParent calls parent in a "safe" way
    function grandParent()
      local ok, val1, val2, val3 = pcall(parent, "key1", "key2")
      if ok then
        -- Use val1, val2, and val3.
      else
        print("ERROR: " .. val1)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a lua file: --functions.lua function testadd(a, b) return a+b end How
I have a C program which uses Lua for scripting. In order to keep
I have a very simple little piece of Lua code, which I wrote while
In C I have a function which registers a callback and a state object
I have a key => value table I'd like to sort in Lua. The
I have a C++ function, called lua_tcall, which extends the abilities of lua_pcall. When
If I have a file called test1.lua function print_hi() print(hi) end and I want
Writing a function in Lua, which creates two tables. I want the tables to
I have a lot of lua-script with the same name used for function name
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.