Discussion:
error handling
Philipp Kraus
2012-12-20 18:46:06 UTC
Permalink
Hello,

I'm working a short time with Lua and primary with the C API, so I would like to know something about the error handling.
Does there anything exists like exception in Lua? Eg the Lua script calls a C/C++ function, on there C++ function an error
occurs and the C++ function throws an exception, I can catch the exception but I don't stop the program, but I would send
the error to the running Lua script, so the script can catch the error and deal with them.

How can I handling errors on the Lua side?

Thanks

Phil
Coda Highland
2012-12-20 18:47:08 UTC
Permalink
On Thu, Dec 20, 2012 at 10:46 AM, Philipp Kraus
Post by Philipp Kraus
Hello,
I'm working a short time with Lua and primary with the C API, so I would like to know something about the error handling.
Does there anything exists like exception in Lua? Eg the Lua script calls a C/C++ function, on there C++ function an error
occurs and the C++ function throws an exception, I can catch the exception but I don't stop the program, but I would send
the error to the running Lua script, so the script can catch the error and deal with them.
How can I handling errors on the Lua side?
Thanks
Phil
LuaJIT on most platforms integrates C++ exceptions with Lua errors, so
pcall() will catch a thrown exception, and try/catch will catch an
error() if it wasn't caught by pcall first. (I think.)

In vanilla Lua, you'll want to explicitly catch the C++ exception and
generate a call to lua_error.

/s/ Adam
Tim Mensch
2012-12-20 18:55:06 UTC
Permalink
Post by Coda Highland
LuaJIT on most platforms integrates C++ exceptions with Lua errors, so
pcall() will catch a thrown exception,
Saying "most" is a bit dangerous. It's important to see the list of
supported platforms here:

http://luajit.org/extensions.html#exceptions

More discussion of LuaJit probably belongs on the LuaJit list; I just
didn't want to leave that unsaid.

Tim
Coda Highland
2012-12-20 19:21:32 UTC
Permalink
Post by Tim Mensch
Post by Coda Highland
LuaJIT on most platforms integrates C++ exceptions with Lua errors, so
pcall() will catch a thrown exception,
Saying "most" is a bit dangerous. It's important to see the list of
http://luajit.org/extensions.html#exceptions
More discussion of LuaJit probably belongs on the LuaJit list; I just didn't
want to leave that unsaid.
Well spoken, thanks for the caveat; that IS important information for
others to know.

/s/ Adam
Roberto Ierusalimschy
2012-12-21 12:18:45 UTC
Permalink
Post by Coda Highland
Post by Philipp Kraus
I'm working a short time with Lua and primary with the C API, so I would like to know something about the error handling.
Does there anything exists like exception in Lua? Eg the Lua script calls a C/C++ function, on there C++ function an error
occurs and the C++ function throws an exception, I can catch the exception but I don't stop the program, but I would send
the error to the running Lua script, so the script can catch the error and deal with them.
How can I handling errors on the Lua side?
[...]
In vanilla Lua, you'll want to explicitly catch the C++ exception and
generate a call to lua_error.
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.

-- Roberto
Philipp Kraus
2012-12-21 12:46:33 UTC
Permalink
Post by Roberto Ierusalimschy
Post by Coda Highland
Post by Philipp Kraus
I'm working a short time with Lua and primary with the C API, so I would like to know something about the error handling.
Does there anything exists like exception in Lua? Eg the Lua script calls a C/C++ function, on there C++ function an error
occurs and the C++ function throws an exception, I can catch the exception but I don't stop the program, but I would send
the error to the running Lua script, so the script can catch the error and deal with them.
How can I handling errors on the Lua side?
[...]
In vanilla Lua, you'll want to explicitly catch the C++ exception and
generate a call to lua_error.
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.
Can I catch this error on the Lua script? In my case a C++ call throws
an exception, I catch them with try-catch, push the error with lua_error to
the stack and the running script should be handle this error.

Phil
Roberto Ierusalimschy
2012-12-21 13:42:40 UTC
Permalink
Post by Roberto Ierusalimschy
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.
Can I catch this error on the Lua script? [...]
Post by Roberto Ierusalimschy
................. In Lua, you can catch such errors with 'pcall'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

-- Roberto
Philipp Kraus
2012-12-21 14:15:02 UTC
Permalink
Post by Roberto Ierusalimschy
Post by Roberto Ierusalimschy
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.
Can I catch this error on the Lua script? [...]
Post by Roberto Ierusalimschy
................. In Lua, you can catch such errors with 'pcall'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sorry, I have confused with lua_pcall (the C fuction). So I just
run a lua function with

if pcall(myfunc()) then
no error
else
error
end

A try-catch around a block does not work, so I must call each "function"
with pcall, do I ?

Thanks

Phil
Glenn Schmottlach
2012-12-21 14:23:40 UTC
Permalink
Something has always bothered me with using C++ to write Lua modules. As I
understand it, the call to lua_error() is implemented using C's
setjmp/longjmp routines which make no effort to do any necessary
"stack-unwinding" which is where C++ does things like calling the
destructors for local (stack) based objects. So if I have such local
objects in my function and call lua_error() will the destructors get called
for these objects? My suspicion is that they will not get invoked unless
Lua is doing something sneaky in it's lua_error() implementation. Does
anyone know the details of this behavior?



On Fri, Dec 21, 2012 at 9:15 AM, Philipp Kraus
Post by Philipp Kraus
Post by Roberto Ierusalimschy
Post by Roberto Ierusalimschy
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.
Can I catch this error on the Lua script? [...]
Post by Roberto Ierusalimschy
................. In Lua, you can catch such errors with 'pcall'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sorry, I have confused with lua_pcall (the C fuction). So I just
run a lua function with
if pcall(myfunc()) then
no error
else
error
end
A try-catch around a block does not work, so I must call each "function"
with pcall, do I ?
Thanks
Phil
Peter Cawley
2012-12-21 14:25:44 UTC
Permalink
If you compile Lua as C, then it'll use setjmp/longjmp, and you don't get
stack unwinding.
If you compile Lua as C++, then it'll use exceptions, and you do get stack
unwinding.

On Fri, Dec 21, 2012 at 2:23 PM, Glenn Schmottlach
Post by Glenn Schmottlach
Something has always bothered me with using C++ to write Lua modules. As I
understand it, the call to lua_error() is implemented using C's
setjmp/longjmp routines which make no effort to do any necessary
"stack-unwinding" which is where C++ does things like calling the
destructors for local (stack) based objects. So if I have such local
objects in my function and call lua_error() will the destructors get called
for these objects? My suspicion is that they will not get invoked unless
Lua is doing something sneaky in it's lua_error() implementation. Does
anyone know the details of this behavior?
Post by Philipp Kraus
Post by Roberto Ierusalimschy
Post by Roberto Ierusalimschy
Or better yet, call lua_error in the first place (instead of throwing
a C++ exception). In Lua, you can catch such errors with 'pcall'.
Can I catch this error on the Lua script? [...]
Post by Roberto Ierusalimschy
................. In Lua, you can catch such errors with 'pcall'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sorry, I have confused with lua_pcall (the C fuction). So I just
run a lua function with
if pcall(myfunc()) then
no error
else
error
end
A try-catch around a block does not work, so I must call each "function"
with pcall, do I ?
Thanks
Phil
Philipp Kraus
2012-12-21 14:44:57 UTC
Permalink
If you compile Lua as C, then it'll use setjmp/longjmp, and you don't get stack unwinding.
If you compile Lua as C++, then it'll use exceptions, and you do get stack unwinding.
Nice to know. How I do this compiler option? Should I setup any preprocessing flags or
should I use a C++ / C compiler only

Phil
Javier Guerra Giraldez
2012-12-21 14:45:33 UTC
Permalink
On Fri, Dec 21, 2012 at 9:44 AM, Philipp Kraus
Post by Philipp Kraus
Nice to know. How I do this compiler option? Should I setup any preprocessing flags or
should I use a C++ / C compiler only
the point to remember is that Lua is written in "clean C", that is the
subset of C that is also valid C++. That allows you to not use
'extern "C" {....}' when including the Lua headers.

That way, the compiler won't switch to C compatibility, and the
preprocessor will be able to define error macros using exceptions
instead of setjmp()/longjmp()

--
Javier
David Favro
2012-12-21 21:44:27 UTC
Permalink
Post by Philipp Kraus
If you compile Lua as C, then it'll use setjmp/longjmp, and you don't get stack unwinding.
If you compile Lua as C++, then it'll use exceptions, and you do get stack unwinding.
Nice to know. How I do this compiler option? Should I setup any preprocessing flags or
should I use a C++ / C compiler only
I believe that C++ exceptions are used for error() by default if the Lua
runtime was compiled with a C++ compiler (not just your C++ functions, but
the Lua distribution); otherwise, if Lua was compiled with a C compiler it
will use setjmp()/longjmp() even if you are using a C++ compiler.

You might want to have a look at:
http://lua-users.org/lists/lua-l/2012-11/msg00509.html

-- David
Roberto Ierusalimschy
2012-12-23 19:08:53 UTC
Permalink
Post by Philipp Kraus
Sorry, I have confused with lua_pcall (the C fuction). So I just
run a lua function with
if pcall(myfunc()) then
no error
else
error
end
A try-catch around a block does not work, so I must call each "function"
with pcall, do I ?
if not pcall(function ()
foo1() -- 1st call
foo2() -- 2nd call
... -- whatever
end) then
error
end

-- Roberto

Loading...