This helped tremendously! I still have some problems which I am hoping are equally easy:
1) Once caught, is there a way to retrieve more info about what caused the problem?
Something like line number and cause of exception (e.g. out of range on GetVi call at line 20)? Right now, everything comes back as "unknown exception". This might be my fault since I added this to my .i file: "%catches (...);" which catches all exceptions on my object, but that was easier than putting specific catches on all the routines of the object.
2) My C++ stuff is doing all its own memory management and sometimes I free stuff which then Lua tries to garbage collect. At least that is what I think is happening since I reuse my Lua state. Is there a way to tell Lua that I own it all? When I debug into my code, I see a check for "own" and the flag is set to false. Is there a way to force to true?
Thanks, Joey
On Mar 12, 2008, at 2:09 AM, Mark Gossage wrote: Hello Joey,
What you are missing is an exception specification. SWIG be default assumes that methods will not throw exceptions unless explicitly stated. Therefore as the method GetVi() doesn't have a specification, SWIG doesn't add the try-catch statement. What happens is that the exception is throw by the vector, is not caught by anything and then causes the failure.
To solve this, please try adding the following code into your .i file:
%include <std_except.i> %catches(std::out_of_range) GetVi;
inline const double GetVi (const unsigned int index) { return _v.at (index); } ... This will make SWIG catch the exception thrown by the vector, and convert it into a Lua error, which can be caught by the pcall. The SWIG manual explains about the exceptions: http://swig.sourceforge.net/Doc1.3/SWIGPlus.html#SWIGPlus_catches http://swig.sourceforge.net/Doc1.3/Customization.html#exception
The SWIG-Lua manual has only just been updated to explain this matter (thanks for pointing out this omission).
Does this help matters? Regards, Mark
Joey Mukherjee <joey@swri.edu> wrote: Thanks Mark and Miles!
Here's an example of what I am wrapping with SWIG:
inline const double GetVi (const unsigned int index) const { return _v.at (index); } inline const double GetXi (const unsigned int index) const { return _xstart.at (index); } inline const double GetYi (const unsigned int index) const { return _ystart.at (index); } inline const double GetZi (const unsigned int index) const { return _z.at (index); }
If index is out of range, it will throw an exception causing my program to core. I have a try/catch (...) around the call to:
errorCode = lua_pcall ((lua_State *) _LState, 0, LUA_MULTRET, 0);
However, my exception handler is never reached.
Should that work for me? I may be missing something...
Thanks, Joey
Sent from Yahoo! Mail. The World 's Favourite Email. |