Why exceptions for error handling is so important

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jan 12 15:01:52 PST 2015


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.


More information about the Digitalmars-d mailing list