Why exceptions for error handling is so important
via Digitalmars-d
digitalmars-d at puremagic.com
Wed Jan 14 03:17:50 PST 2015
On Tuesday, 13 January 2015 at 20:58:43 UTC, deadalnix wrote:
> On Tuesday, 13 January 2015 at 19:36:31 UTC, Marc Schütz wrote:
>> On Monday, 12 January 2015 at 23:01:53 UTC, Ola Fosheim
>> Grøstad wrote:
>>> On Monday, 12 January 2015 at 22:06:32 UTC, deadalnix wrote:
>>>> No, Exception are a bail out mechanism. It is the, I have no
>>>> idea what to do about this mechanism.
>>>
>>> The way it is done in C++, yes.
>>>
>>>> If you put aside performance concerns, exceptions for
>>>> control flow also tend to make many code path implicit and
>>>> makes for very unreadable/unmaintainable code.
>>>
>>> But exceptions are control flow. There is no such thing as
>>> normalized control flow, all state changes implies "control
>>> flow". Think in terms of a state machine. You could just
>>> remove all variables and only have a big state machine
>>> (assuming finite dimensions). Every single state transition
>>> implies flow of control.
>>>
>>> The control flow you see in the source code is just the
>>> programmer's "rendition" of control flow. Exceptions is a
>>> mechanism for getting cluttering resolution out of that
>>> rendition to make the code more readable and maintainable.
>>> The goal is to retain the meat of the computation and remove
>>> the noise.
>>>
>>> Or to put it differently, there are many ways to write the
>>> same function. Good use of exceptions removes the clutter and
>>> leaves the things you care about visible. It's a mechanism,
>>> not a moral issue or a religion.
>>>
>>> So there is nothing wrong with throwing HTTPStatus(409) or
>>> HTTPStatus(201), even though it returns state to the
>>> environment. If that means the code is more maintainable and
>>> more likely to be correct, then that is good use of the
>>> mechanism.
>>
>> I usually don't do this, but I really need to post a "+1" here:
>>
>> +1
>
> Too bad you chose to do this on a bad strawmman.
Your claims:
- Exceptions are for "I have no idea what to do about this"
situations.
- "exceptions for control flow [...] makes for very
unreadable/unmaintainable code"
Ola's answer directly addresses these claims and provides a
counter-example (in the last paragraph), which I happen to agree
with.
=> Not a strawman.
More information about the Digitalmars-d
mailing list