简体   繁体   中英

C++ not catching lua exceptions

I have a C++ program that I have bound to Lua using luabind. I am currently testing the error handling methods that lua and luabind have to offer in order to help with debugging the lua scripts down the road. The objective is to have luabind or lua throw an exception when syntax errors and programming mistakes arise so that I can debug and correct them.

Right now, the problem is that the script below just stops executing without any error messages or exceptions being thrown, so in a larger program I would have no idea where the problem would be or even if there was a problem in the first place.

Here are the relevant snippets:

Lua: (start.lua)

--complete file shown, this is meant to test the error handling of the C++ program
print("This is valid")
print(1234)
bad_function()
a = "meow"
b = 7
c = a + b

C++:

Engine *callbackEngine;
int theCallback(lua_State *L) //This is so I can use my own function as an
                              //exception handler, pcall_log()
{
    return callbackEngine->pcall_log(L);
}

void Engine::Run()
{
    luabind::set_pcall_callback(&theCallback); //my own callback function, 
                                               //redirects to
                                               //pcall_log() below
try {
    luaL_dofile(L, "scripts/start.lua");
}
catch(luabind::error &sError) { //This never gets executed, noted by breakpoints
    theCallback(L);
}
//etc...code not shown

int Engine::pcall_log(lua_State *L)
{
    lua_Debug d;
    lua_getstack( L,1,&d);
    lua_getinfo( L, "Sln", &d);
    lua_pop(L, 1);
    stringstream ss;
    ss.clear();
    ss.str("");
    ss << d.short_src;
    ss << ": ";
    ss << d.currentline;
    ss << ": ";
    if ( d.name != 0)
    {
        ss << d.namewhat;
        ss << " ";
        ss << d.name;
        ss << ") ";
    }
    ss << lua_tostring(L, -1);
    logger->log(ss.str().c_str(),ELL_ERROR);
    return 1;
}

Here is the output when run:

This is valid
1234

The script stops running instead of throwing an exception like I anticipated. Is there a way to control when lua throws an exception or another way to handle errors? I have the logging function setup to produce debugging information, but breakpoints reveal that the catch statement above is not getting executed.

Thank you!

luaL_dofile() is not part of Luabind so I wouldn't expect any Luabind exceptions from it. The handler you set is used/passed when Luabind itself calls something from Lua (using pcall() ). luaL_dofile() is part of the basic Lua code (the L suffix marks it as a library wrapper to simplify calls) and plain C so you'll have to do your own error handling (never used exceptions with Lua/Luabind to be honest) after the call.

Untested, but the following code should do what you expected your code to do:

if(!luaL_doFile(L, "scripts/start.lua"))
    theCallback(L);

If you want to load a Lua script through Luabind, then you can't use luaL_dofile or other regular Lua functions to do it. You have to use Luabind functions. So that would look something like this:

namespace lb = luabind;
int luaError = luaL_loadfile(pLuaState, "scripts/start.lua");
//Check for errors

lb::object compiledScript(lb::from_stack(pLuaState, -1));
lb::call_function<void>(compiledScript); //Call the script.

lua_pop(pLuaState, 1); //Remove the script from the stack. It's stored in our luabind::object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM