[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: try..catch for Lua 5.2?
- From: Duncan Cross <duncan.cross@...>
- Date: Mon, 13 Sep 2010 18:24:40 +0100
On Mon, Sep 13, 2010 at 5:57 PM, David Given <dg@cowlark.com> wrote:
> In addition, I've discovered that in an untyped language, exceptions are
> rather awkward to use, because you can't do this:
>
> @try
> ...
> @catch (TypeOfException e)
> ...
> @catch (AnotherTypeOfException e)
> ...
> @end
>
> Instead you have to do runtime type checks:
>
> @try
> ...
> @catch (e)
> if e:IsTypeOfException() then
> ...
> elseif e:IsAnotherTypeOfException() then
> ...
> else
> @throw e
> end
> @end
How about a syntax something like this:
try
-- ...
catch e, MyException do
-- ...
catch e, MyOtherException do
-- ...
catch e do
-- ...
end
...i.e. catch has an optional second "parameter" which is expected to
be a function that takes the error value and returns a new error
value. If the function returns false, nil or no values, that catch
block is skipped and it goes on to the next one. If not, the catch
block is run and any further ones are ignored. So in this case
MyException() and MyOtherException() would be (most likely global)
functions that take the error value as a parameter, check whether it
is of the right type (by checking the metatable or something) and
return the object if successful or nil if not.
-Duncan