lua-users home
lua-l archive

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


I have now done some further research in other languages, too.

C# as I already wrote returns the last error.

JavaScript also returns the last error:
<https://www.ecma-international.org/ecma-262/6.0/#sec-try-statement>

Java as you wrote returns the first error and logs all further errors:
<https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions>
This would be similar to the warn function, as "nobody" already
suggested in this thread earlier. (I just hope that the warn message
handler gets more flexible than it is at the moment).

C++ terminates the application once a second error happens:
<https://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor>

Lua in xpcall returns "error in error handling":
  xpcall(function () error "1" end, function () error "2" end)
(Yes, that is not an exact match to __close, but still a similar case to
me).

Francisco Olarte wrote:
> IMO __close methods belong to resource/service objects, and it's no
> throw behaviour can normally be unit tested on them, giving a way to
> easily catch errors in them will lead to more difficult code in the
> well written cases and, what is worst, send a message theat "throwing
> in __close" is ok, and instigate people to write __close without care.

I agree that __close should be written very carefully but I am not sure
that ignoring errors will lead to extra careful code in general. So from
the point of view of education the C++ approach might be the best...not
sure I'd like to see that in Lua.

>> Consider this program with two errors, where at least one is a
>> programming mistake:
[...]
> That's true, but IMO a construct like that ( oportunistic closeing
> code written in the same code chunk as the mian one ) would be better
> served by one of the many ways you can simulate a try/finally.

That code was just supposed to be a short example that is easy to run in
an interpreter, the created object should resemble a real object with
resources. (In another answer in this thread I have now written a more
realistic version).

Best regards

David