Phobos 3 Discussion Notes - 02-01-2024

H. S. Teoh hsteoh at qfbox.info
Sun Feb 4 23:16:36 UTC 2024


On Sun, Feb 04, 2024 at 09:10:20PM +0000, Sebastiaan Koppe via Digitalmars-d wrote:
[...]
> I personally find exceptions useful only when I want to jump many
> stack frames at once.

But I thought exceptions were not intended to be used in that kind of
way. They're more like an abort condition to short-circuit the normal
flow of control. If a failure condition is an expected part of the
program, throwing an exception is probably not the best way of
implementing it.


> Overall I find them taxing because they add another possible 'return'
> value to a function. Sometimes its documented, but 9 out of 10 times
> you'll find out at runtime.

But isn't it enough to just have a blanket catch block in the dispatch
code to swallow any exceptions that downstream code might generate? You
don't have to know exactly what it is, you just catch it, abort the
offending task, and move on to the next one?


> Knowing all the possible error states of a function helps me
> enormously in avoiding surprises later on. While it might seem
> overwhelming at first, I find it liberating because the complete state
> space is right in front of me.  I guess it is similar to the benefits
> of local reasoning.
> 
> Note I often write server programs. YMMV with e.g. batch programs.

There are certainly valid considerations about the predictability of
control flow. I.e., if you call a series of functions in a row and they
are all nothrow, you can rest assured that the control flow will reach
the end of the block, and not bail out on you in the middle.

Still, when a fatal error happens deep inside some low-level code and
there's nothing else you can do until you get back to the high-level
business logic, exceptions are very useful. To manually bubble the error
state all the way back up the call stack is just too onerous. It forces
all intermediate levels of code to be dependent on the exact error codes
/ sumtypes so that it can be properly propagated up to the level where
the code has enough context to react to it meaningfully. This increases
coupling between orthogonal code. Using exceptions avoids this spurious
coupling.


T

-- 
People tell me I'm stubborn, but I refuse to accept it!


More information about the Digitalmars-d mailing list