[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Binding C++ functions that 'throw' exceptions
- From: Thomas Breuel <tmbdev@...>
- Date: Fri, 17 Aug 2007 09:04:38 -0700
In fact, the standard Lua distribution also has exception handling
when compiled with C++. The way that is done has a couple of
First, it also compiles Lua with C++ linkage, which means that all
the extensions also need to be compiled with C++ (since, otherwise,
they don't see the Lua API functions). But many extensions simply
won't compile with C++ at all. Second, it disables the setjmp/
longjmp mechanism, which means that existing extensions that use that
would have to be hacked.
So, I think the cleanest overall solution for getting exception
handling into Lua would be to change Lua itself so that when Lua gets
compiled with C++, it catches exceptions, but its API functions would
be surrounded by extern "C", and it continues to also provide the
However, patching Lua is not an option for us; we're creating an open
source project, and we don't want to get into the business of
maintaining a Lua fork. If we need to patch anything, it can only be
the interface generator. Furthermore, any interface generator not
designed to cope with exceptions (i.e., most of them) may need to be
hacked anyway, even if Lua itself has exception handling, because
there may need to be cleanup in the interface code (deallocation of
So, I think it would be great if the base Lua distribution were
updated to handle exceptions better. Basically, what would need to
be done is:
* When compiled with C++, there need to be 'extern "C"' wrapped
around all API function declarations.
* When compiled with C++, there should be try...catch blocks wrapped
around all calls to user functions.
* By default, both exception mechanisms should be enabled (for
compatibility), but users should be able to disable them with -
DLUA_NO_EXCEPTIONS and -DLUA_NO_SETJMP
This would maintain compatibility with the existing C distribution
and it would ensure that, no matter what exceptions get thrown, Lua
remains in a consistent state.
Then, interface generators that cope with C++ need to be reviewed for
whether they are exception-safe, and they can provide additional
facilities to deal with exceptions in a more intelligent way, such as
translating specific exceptions into specific Lua errors, etc.
BTW, note that many existing libraries use exception hierarchies that
are not based on std::exception, so just catching std::exception is
not sufficient. The std::exception class is simply the base class for
exceptions in the standard library.
On Aug 16, 2007, at 7:54 PM, Erik Cassel wrote:
Perhaps this is relevant:
"This lets me throw an std::exception-derived object from anywhere
in C++ code. Lua will catch it and translate it appropriately in
Also, Lua uses an std::exception-derived class to throw Lua errors.
This means I can catch errors thrown by Lua (as in
Sample source code is here:
Greg Falcon found a bug in my solution, which he documents here:
Fortunately, the bug
can be fixed by defining a copy constructor. If you're interested I
the code that we use in production.
[mailto:email@example.com] On Behalf Of RJP
Sent: Thursday, August 16, 2007 11:01 AM
To: Lua list
Subject: Binding C++ functions that 'throw' exceptions
I am using toLua++ to bind a bunch of functions and many of them throw
exceptions. Lua / toLua doesn't allow the exception to come back to
after it happens. Instead Lua / toLua reports "Cannot convert value to
string" which is not useful. I need to have the exception to go to
application that knows how to deal with that type of exception. Or
I need to
have the exception re-bundled as a lua_error. I am using Gcc for the
compiler on Windows. If I don't compile Lua as C++ I don't get
except a close of my program. It appears the Gcc has a problem
gap between C and C++ when exceptions are thrown. Are there any
ideas on if
this can be automated? I don't want to write all the bindings by
hand and I
have to deal with exceptions.
Now I have created bindings by hand and I can properly catch the
inside the binding function. All I do with the exception is get the
exceptions description and call luaL_error( description ). This works
flawlessly. I basically am asking if there is a way to make the
functions generate try/catch blocks in the generated functions. Or
the error message from my exception on to the stack so that when
returns it's error number the message will be on the stack as it
I hope this made sense. Please ask if you need clarification.