lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Tim Conkling has...
> a couple of questions regarding error handling of Lua 5 from a 
> host application:

> How can I go about handling errors that occur in the lua_dofile 
> function? If the function fails, it outputs to stderr (which I'd like 
> to prevent) and provides no indication that failure has occurred. Is 
> there any way I can suppress the output of the error message, discover 
> whether an error occurred, and retrieve the error string that would 
> normally be written to the console upon failure?

Don't use lua_dofile. Use luaL_loadfile() to load the file. It will
report syntax errors. If it doesn't, you can then use lua_pcall() or
one of its friends to execute the chunk.

> The functions that I expose to my Lua environment from the host app do 
> error checking. Currently, if a function is called incorrectly (with 
> bad arguments), all I can do is report that an error has occurred -- I 
> can't tell which line in which function caused the error. Is there any 
> way to get this information?

Yes. See the debug interface.

However, it is easier to just call lua_error and let the standard
traceback function do the work for you. You can find the code for
this function in ldebug.c if you want to roll your own for some reason.

Look at the Lua standalone interpreter (lua.c) to see how this all fits
together in practice. You can probably cut and paste from lua.c into
your host application.