lua-users home
lua-l archive

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


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.