Catching Errors when your Lua Script doesn’t start
Getting an error code back when a Lua script dies is actually pretty straightforward. Use lua_pcall() or one of its relatives to call the script and the return value will tell if there's been a run time error. lua_State *L; L=lua_open(); luaopen_base(L); // load basic libs (eg. print) Simple_Init(L); int nScriptError; nScriptError = luaL_loadfile(L,SCRIPT_FILENAME); if (0 == nScriptError) { nScriptError = lua_pcall(L,0,0,0); switch (nScriptError) { case LUA_ERRRUN: cout << "Runtime error occured." << endl; break; case LUA_ERRMEM: cout << "Memory allocation error while running the script." << endl; break; case LUA_ERRERR: cout << "Error handler failed." << endl; break; case 0: cout << "Script complete." << endl; break; default: // not supposed to happen cout << "An unknown error has occurred while running the script." << endl; break; } // if an error occurs then there should be a message on the stack if ((0 != nScriptError) && (lua_isstring(L,1))) { cout << lua_tostring(L,1) << endl; } } else { switch (nScriptError) { case LUA_ERRFILE: cout << "Unable to load " << SCRIPT_FILENAME << endl; break; case LUA_ERRSYNTAX: cout << "Syntax error while precompiling " << SCRIPT_FILENAME << endl; break; case LUA_ERRMEM: cout << "Memory allocation error while loading " << SCRIPT_FILENAME << endl; break; default: // not supposed to happen cout << "An unknown error has occurred while loading " << SCRIPT_FILENAME << endl; break; } } lua_close(L); This little snippet will load up a script and run it. If something goes wrong you'll get a message, not just a blank stare. This sort of stuff, detailed error-handling, is often left for the end but I believe it's very useful to developers and while the project is being built. The earlier you build-in explicit, clear error codes the more robust the application will be. I've seen many cases where a program is designed and built then error-checking is bolted on at the end. What ends up happening is that there's no communication channel for the error information to reach the right end-point. Instead message boxes start popping up for the user and the program can't cope with problems that could have been handled internally. But I digress. The first switch in the above code checks for errors that occur while running the script. The second switch is concerned with diagnosing an error that happened while loading the script from a file. The information provided isn't very detailed. This might be sufficient for some cases, depending how large and involved your codebase is. There are debuggers that will help you through Lua code which may reduce the need in some cases for more detailed information. Still, we usually like to at least see a stack trace. This can be achieved by supplying an error-handler function to pcall. While the documentation for lua_pcall() mentions the idea of dumping a stack trace for a runtime error, there don't seem to be many accessible examples of doing this. I found a thread about lua_pcall() error functions on a mailing list that seems to have some clues about a function that's part of the Lua source which may do what I want. I'll do a little more digging and elaborate if I find anything good.
Your rating: None

I think you have an extra right-brace after the first if. This could be easily detected if you embed some indentation in your code samples (nbsp;nbsp; I guess - if you figure out a better way, let me know).

Nope - I was wrong - your code is correct (didn't see the other if you had there). My bad.