[OT] - C++ exceptions are becoming more and more problematic

Walter Bright newshound2 at digitalmars.com
Fri Feb 25 07:29:42 UTC 2022


On 2/24/2022 3:44 PM, H. S. Teoh wrote:
> On Thu, Feb 24, 2022 at 02:42:14PM -0800, Walter Bright via Digitalmars-d wrote:
>> On 2/24/2022 12:01 PM, H. S. Teoh wrote:
>>> The problem with replacing exceptions with return codes is that it
>>> requires writing tons of boilerplate to handle error codes.
>>
>> I specifically said not using return codes, and gave examples.
> 
> It amounts to the same thing: the caller must explicitly handle all
> error conditions.

Not if you do it right. It's a challenge, I know.

The NaN result from floating point errors is an example. You can continue using 
the value without checking - the error nicely propagates with no effort or cost.


> Also, redesigning the problem like using the replacement character on
> invalid UTF-8 does not always make sense.  Sometimes you *want* your
> code to abort upon failure rather than silently substitute a nonce value
> in your data. E.g., if you're deep inside some parsing function and
> encounter a UTF-8 encoding problem, you do not want it to just insert
> some replacement data that isn't part of the input stream; you want it
> to bail out and abort the entire operation with a proper error message.

Interestingly, it seems one almost never wants to do this. For those that do, 
running a validator over your string first is a reasonable solution.

We do another technique inside D. The error is printed when the error is found, 
then a special AST error node is created, which is just ignored by everything 
else. It works out reasonably well.



More information about the Digitalmars-d mailing list