|
在 2011-6-20 晚上10:25,"Roberto Ierusalimschy" <roberto@inf.puc-rio.br>写道:
>
> > I just found that the error/pcall/xpcall in lua is just like exceptions, you
> > can report a error, and catch it like this:
> >
> > function foo()
> > ...
> > if notReady then error"not ready" end
> > ...
> > end
> >
> > if pcall(foo) then
> > ...
> > end
> >
> > but I still have some question about this Idiom:
> >
> > 1. how can I make sure some operations MUST happen?
> > xpcall may be can do this, but the errfunc may not called if f call success.
> > some pseudo-code:
> >
> > [...]
>
> The name "error function" is not very precise (we changed it to "message
> handler" in 5.2). The goal of this parameter to xpcall is not to treat
> the error, but to treat the error message (e.g. adding a traceback to
> it).
>
> The right place to handle errors is after pcall (or xpcall) returns, as
> in your first example:
>
> if pcall(foo) then
> <error handling code>
> end
>
> So, to make sure that some operations MUST happen just put them out of
> the 'if':
>
> if pcall(foo) then
> <error handling code>
> end
> <MUST happen code>
>
> -- Roberto
>
Okay ... thank you very much... but all I want is _pass_ error out, i.e. I want spread error, let my caller, or my caller's caller etc. To handle the error. I don't know how to process the error In my call, so I throw it away.
But I have some things to do even if I want throw the error, I want to execute some code when the function I called have error, even if I don't care what error it does , I just want some resource to be clean .
C++ has RAII, Java has finally statement, is there any easy way to do it in lua?