[Cfp-interest] try/catch in C/C++

David Hough CFP pcfp at oakapple.net
Fri May 30 12:19:54 PDT 2014


I think Ian's point is well taken that we should consider a uniform way to
define try/catch in C and extend it in C++ so that similar capabilities
are avaiable in similar ways in both languages, for implementations that
choose to offer them.



Considering both the possibility of adding C++ try/catch to C, and of
adding C floating-point support to C++, suggests that there should be
one try clause and multiple kinds of catch:

try {
}
catch_excep( e, exceplist) {
/* catch fp exception immediate, don't return */
/* In C++, an implementation extension if defined #include <something>
C++ should warn if #include <something> not defined */
}
catch( type param) {
/* C++ style thrown or standard exception
In C, an implementation extension if defined #include <exception>
C compiler should warn if #include <exception> not defined */
}
catch( ... ) {
/* C++ style thrown or standard exception default handler
In C, an implementation extension if defined #include <exception>
C compiler should warn if #include <exception> not defined */
}


 
catch_flag( f, flaglist) {
as previously proposed does not interrupt execution of the try block,
so is not a control transfer and could perhaps better be handled
by a pragma.    And it's just syntactic sugar for something
that can be done verbosely with the existing flag operations.
 
catch_signal( s, signallist ) {
could be an extension for C or C++, to make signal handling as uniform
and convenient as floating-point exceptions.


None of these catches allow a return to the try block.

Perhaps exception handling that doesn't change control flow is best
left to pragmas as previously discussed:

#pragma STDC FENV_EXCEPT exception-list action

where action could be, as before
        DEFAULT
        NO_FLAG
        MAY_FLAG
        ABRUPT
        RECORD

That leaves presubstitution.    How about

#pragma STDC FENV_EXCEPT exception-list SUBSTITUTE (expression)

where expression is required to be invariant within the compound statement
controlled by the pragma, for determinacy.


The #pragma syntax is better perhaps for file-scope or function-scope
declarations, the try/catch better for local ones.     We anticipate other
pragmas for other purposes to implement 754-2008 directives.
The try/catch paradigm could be extended to have catch-phrases for other
directives that don't necessarily change control flow, and that would
be more attractive syntatically than #pragma, but perhaps it's better to
start with what's really essential and leave expansion to further
generations.



More information about the Cfp-interest mailing list