Re: Catching exceptions

Ryan Pavlik <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
On Fri, Apr 29, 2011 at 11:28 AM, Josh Klint <[email protected]> wrote:

> I created a C++ function to throw an exception:
>
>
>
> void RuntimeError(const std::string& error)
>
> {
>
>       throw std::exception(error.c_str());
>
> }
>
>
>
>
>
> Here is the Lua script that calls it:
>
>
>
> print("Creating an error...")
>
>
>
> RuntimeError("An error has occurred!")
>
>
>
>
>
> Here is the code that runs the script.  Instead of catching the exception,
> lua_pcall returns non-zero and the Lua string at the top of the stack
> contains "std-exception: 'An error has occurred!'":
>
>
>
> bool Interpreter::Invoke(const int& in, const int& out)
>
> {
>
>       try
>
>       {
>
>             if (lua_pcall(L,in,out,0)==0)
>
>             {
>
>                   return true;
>
>             }
>
>             else
>
>             {
>
>                   //If you throw an exception, it gets displayed here,
> which isn't what we want:
>
>                   HandleError();
>
>             }
>
>       }
>
>       //Exceptions should be getting caught here:
>
>       catch(luabind::error& e)
>
>       {
>
>             Print("Caught the exception!");
>
>             exit(1);
>
>       }
>
>       return false;
>
> }
>
>
>
>
>
> I am using MSVC, and I know about the issue described below.  The code they
> said to add has no effect on behavior:
>
>
> http://www.rasterbar.com/products/luabind/docs.html#structured-exceptions-msvc
>
>
>
> What do I have to do to make my program catch the exception thrown by the
> C++ function the Lua script calls?  Thanks.
>

If you have built Lua as C (which is typical) rather than C++, exceptions
can't be thrown "through" stack frames with Lua calls - it wouldn't work.
So, Luabind catches all exceptions from C++ code called by Lua code, then
propogates them as Lua errors (which is what you're seeing - message on top
of the stack, etc). See
http://www.rasterbar.com/products/luabind/docs.html#exceptions

<http://www.rasterbar.com/products/luabind/docs.html#exceptions>Ryan

-- 
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University

[email protected]
http://academic.cleardefinition.com
Internal VRAC/HCI Site: http://tinyurl.com/rpavlik

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network 
management toolset available today.  Delivers lowest initial 
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.