[dmd-internals] Throwing Errors

Don Clugston dclugston at googlemail.com
Mon Mar 12 13:29:25 PDT 2012


On 12 March 2012 20:34, Sean Kelly <sean at invisibleduck.org> wrote:
> On Mar 9, 2012, at 9:38 PM, Walter Bright wrote:
>>
>> On 3/9/2012 6:29 PM, Jonathan M Davis wrote:
>>> The issue of catching Errors came up in D.learn today, and I need some
>>> clarification. It has been my understanding that anything which is thrown which
>>> is not derived from Exception skips all destructors, scope statements, and
>>> finally blocks, meaning that it's generally unsafe to catch them, because your
>>> program is potentially in an invalid state. However, it was brought to my
>>> attention that the compiler does not currently follow this behavior - all 3 of
>>> those get run for Errors at present. So, the question is whether I'm just
>>> completely misunderstanding something or whether something has changed.
>>>
>>> Is it guaranteed that all thrown Throwables will result in all destructors,
>>> scope statements (exit and failure at least), and finally blocks that they pass
>>> being executed? Or is it only guaranteed for Exception and its derived types
>>> and just so happens to work for other exception types right now?
>>
>> It just so happens to work. It's wrong.
>
> I'm on the fence about whether attempting cleanup when an Error is thrown is desired behavior.  If there is no cleanup, why allow Errors to be caught at all?  We may as well simply call abort() at the point they're thrown.

I went to a lot of trouble to ensure the exception chaining behaviour
of Errors vs Exceptions.

Personally, I just don't buy the argument that the entire system is in
an invalid state when an Error occurs. It's a sign that some subsystem
is in a invalid state, but how much of the whole app is invalid,
depends on the particular situation. For example, if you're
extensively using class invariants and contracts, and you get an
AssertError, then the possible damage is pretty small. Especially if
it is an assert failure in a pure @safe function; cleanup will be
perfectly safe in that situation.

I would have thought that if you had a truly fatal error, you would
have called exit() or something, instead of throwing an Error.


More information about the dmd-internals mailing list