Alternatives to exceptions for error handling
Ali Çehreli
acehreli at yahoo.com
Mon Nov 30 11:02:29 UTC 2020
On 11/30/20 1:58 AM, Gregor Mückl wrote:
> On Sunday, 29 November 2020 at 23:20:13 UTC, Roman Kashitsyn wrote:
>> On Sunday, 29 November 2020 at 19:52:27 UTC, Jacob Carlborg wrote:
>>> [..]
>>
>> Sure, it's not always possible to handle errors at the level they
>> arise, those need to be propagated.
>> On the other hand, most of the time the caller has even less clue on
>> how to deal with the error: the further you go from the point where
>> error happened, the less likely it's going to be handled in a
>> meaningful way.
>>
>
> What kind of error conditions are you talking about that you consider
> handleable locally? Do you have concrete examples? I am asking because
> this is way outside the experiences I have made regarding error handling
> and I would like to understand your perspective.
>
> Every serious application that I have ever worked on had to deal with
> errors in the context of larger operations. There were essentially no
> locally handleable errors. So every error has to go up a few layers
> until there is even enough context available for recovery. This happens
> for example when an change operation triggers a sanity check deep inside
> a complex data model. The failing check has no notion of whether the
> larger operation needs to be rolled back or whether this is expected by
> the controller and there is a fallback strategy.
>
That is exactly my experience as well.
My programs catch Exception type in main() to report a user friendly
message. (Error is not caught.) There are a couple of exception to this:
- I catch and ignore errors locally in non-essential operations like
printing verbose output or collecting timing statistics. (Nobody cares
if they failed and everybody is mad if they failed.)
- I catch formatting errors to augment the error because a higher level
may not understand a low level error of "cannot convert 'x' to int".
That's all... Exceptions are the simplest error management. For me,
their only problem is the performance impact, which I haven't even
measured. :)
The code is the cleanest with exceptions: enforce() and assert() checks
guarantee that everything is sane. If not, the operation is aborted.
Ali
More information about the Digitalmars-d
mailing list