Catching exceptions

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

Catching exceptions

Josh Klint

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.


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Catching exceptions

Ryan Pavlik-4


On Fri, Apr 29, 2011 at 11:28 AM, Josh Klint <[hidden email]> 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

Ryan

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

[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Catching exceptions

Jason McKesson
In reply to this post by Josh Klint
On Apr 29, 2011 9:28 AM, Josh Klint 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.


You have to use Luabind to call the script. If you use Lua functions to execute the script, then there will be no Luabind in the way to catch the exception and convert it.

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user