Advertisement

Lua and Errors

Started by September 04, 2004 11:06 PM
5 comments, last by brain21 20 years ago
Hey, is there any way to get lua to report compile or syntax error to the host program? I've used the panic function and that works for some errors, but if I try to call a function that does not exist, then it doesn't work. Instead it just (I guess) outputs the error message ("trying to call nil value") to the stdout. Are there any way of catching these errors? Also, I while back I saw someone post some code somewhere that would print out the globals table and it's values. Is there any easy way of doing this? can you post some code please? Thanks all. Denny.
Check out the lua_pcall function.
Advertisement
As for the second question:

table.foreach(_G, print)

table.foreach is an algorithm which takes a table, and a function. It iterates over the table, calling the function with each key/value pair. Print, of course, just prints out whatever is passed in. _G is an alias for the global table.
Thanks for the second part, I'll try it soon. For the first part, lua_pcall is only for calling functions. what if I do lua_dofile? then my lua script looks like this:

--Lua Test Program
Brain21( "this causes an error" )
--End file

there is no function Brain21 defined, so it will give me an error, but not one that I can catch (so far). Is there a way?

Denny.
lua_dofile is kind of deprecated. You should prefer luaL_loadfile, followed by lua_call (or lua_pcall).
In Lua 5.0 register a function called _ALERT.

This function will get called with the error string as the only parameter.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Advertisement
Thanks, both of you! I will try thoses soon. I must have an outdated LUA manual. I just looked at their HTML manual and its different than mine.

Thanks again.

Denny.

This topic is closed to new replies.

Advertisement