[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Exceptions (was Re: Custom extensions to Lua)
- From: Diego Nehab <diego@...>
- Date: Thu, 11 Aug 2005 23:52:59 -0400 (EDT)
<exp> --> ... | <blockexp>
<blockexp> --> block <Name> <block> end
Don't you think it can get quite complicated to trace where these
blocks reside once you start passing them around?
Mark's idea solves a simple problem that is very common. If there was a
way to do what he proposes, we could severely reduce the number of lines
of code in programs that need to perform lots of task, and check if each
one of them failed.
The exceptions mechanism we use in LuaSocket could be used for
hard-errors, this return-on-error could be used for soft-errors.
All we need to do is come up with a finalization mechanism that is safe
and we are all set. For return-on-error this would be symple:
c = socket.connect(..)
roe = newroe(function() c:close() end)
Now if use() fails, roe not only returns "nil, error", but it also calls
the finalizer to close he socket. The problem with the newtry method I
use in LuaSocket is that exceptions are not aware of each other.
c = socket.connect(...)
try = newtry(function() c:close() end)
If use() returns "nil, error", all is well. Try() will detect the failure
and call c:close(). But if use() throws an exception, try() is not even
called, so it doesn't get a chance to finalize appropriately.
Of course we could protect the call to use(), but this would be very
cumbersome. What we need is a way to catch exceptions and transform them
into a "nil, error" pair to be passed to try(). One way to do this would
be to flag the function somehow so that nested calls are automatically
performed with pcall.
So here is the deal, if we add a mechanism to do return-on-error
and another to protect nested calls, we will have a very simple,
elegant, and generic error propagation system for Lua.
What do you guys think?