# Lua and error line number

This topic is 3644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I am using Lua 5.1. When I call my Lua program with lua_pcall and an error occured, I retrieve the error string with lua_tostring(L,-1). The strange thing is that sometimes the string indicates the line number where the error occured, sometimes it doesn't. How come? For instance, following Lua code: a causes following error string: attempt to call a string value But following Lua code: a=a+1 causes following error string: [string "NameOfScript"]:1:attempt to perform arithmetic on global 'a' (a nil value) The second error string contains information about where the error occured. How can I always have that information? (or am I doing something wrong?)

##### Share on other sites
You can do that by using the last argument to the lua_pcall-function. That is the index to a Lua-function that will format the error-message to your needs. Most convenient is to use debug.traceback().

So, this would become something like:
lua_getglobal(L, "debug");        // push debug-table on the stacklua_getfield(L, "traceback");     // push debug.tracebacklua_getglobal(L, "yourFunction"); // push your functionlua_pushstring(L, "an argument"); // push argument to your functionif( lua_pcall(L, -2, 1, 0, -3) )  // call function at -2, with -3 as errfunc{   fprintf( L, "Lua error: %s\n", lua_tostring(L,-1) );}lua_pop(L,2);

##### Share on other sites
are you using decoda at all? it binds itself to the vis debugging symbols and runs in debug mode so allows you to put breakpoints within your script file so you can track whatever this global a is doing in more detail maybe

##### Share on other sites
Your solution however still not solves my problem. Following program:
a

now generates:

"stack traceback:
attempt to call a string value"

"attempt to call a string value"

Am I missing something or doing something wrong?
This is how I make my call:

lua_getglobal(L,"debug");lua_getfield(L,-1,"traceback");luaL_loadbuffer(L,scriptText,scriptTextLength-1,getScriptName().c_str());inputArguments.pushObjectsOnStack(L);if (lua_pcall(L,inputArguments.getObjectNumber(),LUA_MULTRET,-inputArguments.getObjectNumber()-2)!=0)    	printf("%s \n",lua_tostring(L,-1));

##### Share on other sites
Strange I don't that error when executing that sample script.

a

[string "Lua"](1): '=' expected near '<eof>'

Are you sure u haven't bound the variable a to some function ptr?

Good Luck!

-ddn

##### Share on other sites
I know what the problem is, it got me as well. The problem is

can fail, due to compilation errors. There are 2 types of errors, compilation and run time errors. When u execute the script after a compilation error there is only a string on the stack, the error string. So you get this obscure error "attempt to call a string value".

The real error of coruse is that string, which was pushed onto the stack on compilation failure. Get check if luaL_loadbuffer fails( i forget what it returns if it fails, its all in the documentation ), if so it pushes the error string on the stack and you'll need to pop it off and that will give you the line number and everything.

Good Luck!

-ddn

##### Share on other sites
ddn3, Thanks a lot!!
That was a very stupid error but somehow never thought about that. Working like a charm now.
Thanks a lot for your help :)

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 86
• 11
• 10
• ### Forum Statistics

• Total Topics
632972
• Total Posts
3009613
• ### Who's Online (See full list)

There are no registered users currently online

×