:: Re: [DNG] C exception handling
Top Page
Delete this message
Reply to this message
Author: Didier Kryn
Date:  
To: dng
Subject: Re: [DNG] C exception handling
Le 27/10/2024 à 20:58, karl@??? a écrit :
> exceptions:
> I saw in some ADA context it was inspired by the programmers
> lazyness at checking return value. It seems to be a good way to
> catch divide by zero, bound checking etc. longjmp is a very poor
> replacement for that, but if it important enought you can always
> do the checking yourself:
> if (b == 0) { val = INT32_MAX; } // or whatever
> else { val = a/b; }


    It's not at all lazyness. The issue is that all these checks,
together with their associated conditional branchings are intermixed
with the normal algorithm which may be already complicated enough, 
implying itself conditional branchings. This makes the algorithm harder
to read. The normal algorithm and the management of exceptions are two
different things; they should not be intermixed because this makes the
program just unreadable. The quality and maintainability of a program is
closely related to its readability.

    The distinct advantage of using a CPP macro, with respect to a
function, is that a macro can make a branching inside the current
function, eg. (if( (q=myfunc()) ) goto exception); . This is a way to
hide the detection of exceptions and the branchings to the reader. If
you want to do that from a function, then you need setjmp/longjmp, which
means being very carefull with dynamic allocations and stack usage. But
I persist: these macros are just tricks. I can tell you it's a big
breath of air to program in a language which offers you a smart
management of exceptions, and not only that, of course.

--     Didier