Best method for printing errors?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Best method for printing errors?

Thomas Daldier
Hello, I'm a bit (a lot bit) new to binding C/C++ with Lua (I've only been doing this for about a week), and I would like a bit of help on Lua error handling:

I've been working on a game engine in C++, and I want Lua to print every error it comes across. The only way I can get an error message (it seems) is if Lua comes across a fatal error that causes Lua to abort the script completely.
What is the best way to print Lua errors?

I've searched on Google and in the documentation, but I can't seem to find exactly what I'm looking for.

If somebody could give me a simple example or link me to something that shows me how to do this, that would be nice.

Thanks, Sgt. Sparky.



Store, manage and share up to 5GB with Windows Live SkyDrive. Start uploading now
Reply | Threaded
Open this post in threaded view
|

Re: Best method for printing errors?

Eva Schmidt
Hi,

one way to print errors, but avoid the abortion is to use Lua's error () function together with pcall (). pcall () catches the error and returns the message.
Maybe the assert() function could also be helpful to you.

Further I found this presentation from John Belmonte about exception patterns in Lua:

www.lua.org/wshop06/Belmonte.pdf

Maybe this helps :-)
Kind regards,
Eva

Thomas Daldier wrote:
Hello, I'm a bit (a lot bit) new to binding C/C++ with Lua (I've only been doing this for about a week), and I would like a bit of help on Lua error handling:

I've been working on a game engine in C++, and I want Lua to print every error it comes across. The only way I can get an error message (it seems) is if Lua comes across a fatal error that causes Lua to abort the script completely.
What is the best way to print Lua errors?

I've searched on Google and in the documentation, but I can't seem to find exactly what I'm looking for.

If somebody could give me a simple example or link me to something that shows me how to do this, that would be nice.

Thanks, Sgt. Sparky.


------------------------------------------------------------------------
Store, manage and share up to 5GB with Windows Live SkyDrive. Start uploading now <http://skydrive.live.com/welcome.aspx?provision=1?ocid=TXT_TAGLM_WL_skydrive_102008>


Reply | Threaded
Open this post in threaded view
|

RE: Best method for printing errors?

Thomas Daldier
Thanks for the reply, but I'm not sure this is exactly what I'm looking for:

> one way to print errors, but avoid the abortion is to use Lua's error ()
> function together with pcall (). pcall () catches the error and returns the
> message.
> Maybe the assert() function could also be helpful to you.

These functions are all called from Lua, I need something that Lua will call whenever it comes across an error.

> Further I found this presentation from John Belmonte about exception patterns in
> Lua:
>
> www.lua.org/wshop06/Belmonte.pdf

Thanks for the link to the presentation, it had a nice list of methods. :)


Is there some sort of function That I could register in Lua or as a hook to print out every single error it comes across to the console?

My application works like this:
1. It gets the file name from the start-up arguments. If one isn't listed, it uses a default file name.

2. It registers all my functions/tables to to the lua state. (HGE, EveR [ my C++ game engine that uses Box2D physics. ], and a few other things)

3. It runs the specified file.

4. It quits after the file is through running. (It doesn't have anything better to do.)

Thanks,
--Sgt. Sparky

:) :) :)


See how Windows Mobile brings your life together—at home, work, or on the go. See Now
Reply | Threaded
Open this post in threaded view
|

Re: Best method for printing errors?

Wesley Smith
>From what I can tell, it's exactly what you're looking for.  lua_pcall
allows you to register an error function that gets triggered upon
error.  If it's a C function, simply push it onto the stack to make it
available to Lua.
wes

On Tue, Nov 4, 2008 at 1:09 PM, Thomas Daldier <[hidden email]> wrote:
> Thanks for the reply, but I'm not sure this is exactly what I'm looking for:
>
>> one way to print errors, but avoid the abortion is to use Lua's error ()
>> function together with pcall (). pcall () catches the error and returns
>> the
>> message.
>> Maybe the assert() function could also be helpful to you.
>
> These functions are all called from Lua, I need something that Lua will call
> whenever it comes across an error.
>
>> Further I found this presentation from John Belmonte about exception
>> patterns in
>> Lua:
>>
>> www.lua.org/wshop06/Belmonte.pdf
>
> Thanks for the link to the presentation, it had a nice list of methods. :)
>
>
> Is there some sort of function That I could register in Lua or as a hook to
> print out every single error it comes across to the console?
>
> My application works like this:
> 1. It gets the file name from the start-up arguments. If one isn't listed,
> it uses a default file name.
>
> 2. It registers all my functions/tables to to the lua state. (HGE, EveR [ my
> C++ game engine that uses Box2D physics. ], and a few other things)
>
> 3. It runs the specified file.
>
> 4. It quits after the file is through running. (It doesn't have anything
> better to do.)
>
> Thanks,
> --Sgt. Sparky
>
> :) :) :)
>
> ________________________________
> See how Windows Mobile brings your life together—at home, work, or on the
> go. See Now


Reply | Threaded
Open this post in threaded view
|

RE: Best method for printing errors?

Thomas Daldier

> >From what I can tell, it's exactly what you're looking for. lua_pcall
> allows you to register an error function that gets triggered upon
> error. If it's a C function, simply push it onto the stack to make it
> available to Lua.
> wes
>

Wow, it was what I needed. Thanks!
 
I feel quite dumb right now, I was looking at it from the wrong perspective. It was exactly what I needed. 
Sorry, next time I won't be so quick or careless about what I say I don't need.
 
I have it all working now. :)
 
Thanks,
--Sgt. Sparky


Want to do more with Windows Live? Learn “10 hidden secrets” from Jamie. Learn Now