|
|
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
|
|
On Thu, Dec 20, 2012 at 10:46 AM, Philipp Kraus
< [hidden email]> wrote:
> 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
|
|
On 12/20/2012 11:47 AM, Coda Highland wrote:
> 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#exceptionsMore discussion of LuaJit probably belongs on the LuaJit list; I just
didn't want to leave that unsaid.
Tim
|
|
On Thu, Dec 20, 2012 at 10:55 AM, Tim Mensch < [hidden email]> wrote:
> On 12/20/2012 11:47 AM, Coda Highland wrote:
>>
>> 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.
Well spoken, thanks for the caveat; that IS important information for
others to know.
/s/ Adam
|
|
> > 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
|
|
Am 21.12.2012 um 13:18 schrieb Roberto Ierusalimschy:
>>> 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
|
|
> > 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 Lua, you can catch such errors with 'pcall'.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- Roberto
|
|
Am 21.12.2012 um 14:42 schrieb 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? [...]
>
>>> ................. 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
|
|
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 <[hidden email]> wrote:
Am 21.12.2012 um 14:42 schrieb 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? [...]
>
>>> ................. 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
|
|
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 <[hidden email]> wrote:
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 <[hidden email]> wrote:
Am 21.12.2012 um 14:42 schrieb 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? [...]
>
>>> ................. 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
|
|
Am 21.12.2012 um 15:25 schrieb Peter Cawley:
> 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
|
|
On Fri, Dec 21, 2012 at 9:44 AM, Philipp Kraus
< [hidden email]> wrote:
> 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
|
|
On 12/21/2012 09:44 AM, Philipp Kraus wrote:
>
> Am 21.12.2012 um 15:25 schrieb Peter Cawley:
>
>> 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
|
|
> 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
|
|