lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


* Daurnimator <quae@daurnimator.com> [11/12/24 00:30]:
> It certainly is.
> Just add a useful __tostring to your error objects.
> A possibly useful piece of code:
> local err_methods = { }
> local err_mt = {
> 	__index = err_methods ;
> 	__tostring = function ( o ) return o:tostring ( ) or "No error message" end ;
> }
> 
> function new_err ( t )
> 	return setmetatable ( t or { } , err_mt )
> end

This sounds interesting. One would need to re-define 'error()'
built-in as well, to be able to "throw" such objects, no?

We have a somewhat similar problem in Tarantool/Box and use a
similar solution. Earlier I wrote that to throw an error,
we've reset lua_atpanic(), and it now throws a C++ exception. 

Apart from that, it's possible to construct and throw a wholesome
C++ exception from Lua using a few bindings:

e = box.error.new(0x02001 -- error code,
                  'Space is disabled')
-- the above produces a userdata which links to a C++ object
box.error.throw(e) -- this simply calls into C++ throw.

The pcall() in turn is reset to do a catch and return the above
mentioned userdata object instead of a Lua string with an error
message.

This approach does solve the problem of throwing more elaborate
errors, but Lua is still missing something like 'finally'
clause, and this can't be easily fixed.

-- 
http://tarantool.org - an efficient, extensible in-memory data store