[Cfp-interest] further thoughts on exception clause syntax

David Hough CFP pcfp at oakapple.net
Mon Jun 2 16:06:24 PDT 2014



Rajan points out that C is really intended to be a one-pass compilable
language.    Mike points out that there's no necessity for a try block
if you can attach exception catches to any syntactic block.    So here's
a summary of my thinking so far; please respond with your comments:



try/catch constructs that differ in some details are found in C++, Java,
Perl, PHP, Python, Matlab, and at least two Fortrans for .NET.
Ruby has something that is somewhat different
syntatically: there is no try keyword; catch clauses (keyword "rescue")
can be attached to any compound statement.

Floating-point exceptions are different from
all these other schemes, for which the presence
or absence of catch clauses does not affect how the try clause is compiled.
However in the case of floating-point exceptions to be detected ASAP
by checking operands or flags, then that checking code has to be compiled
into the try clause, just for the specific exceptions that the programmer
has asked to be detected - one doesn't want to waste time detecting inexact
or underflow exceptions unless the programmer asks.
>From this point
of view, the common syntactical convention of try followed by catch is
a bad choice for one-pass compilers.

What could be done instead?     One could imagine having the catch_excep
clauses before the try -
and since they are now so syntactically different from C++ etc catch,
we might as well leave out the "catch" and just call them "excep":


 
excep () {
 ... exception code
}
try {
 ... try code
}
 
or perhaps

try {
excep () {
 ... exception code
}
 ... try code
}


And since we're deviating further from C++ syntax anyway, maybe just
leave out the explicit try and attach to any compound statement:
 
excep() {
 ... exception code
}
{
 ... try code
}
 
or
 
{
 excep() {
 ... exception code
}
 ... try code
}

To attain one-pass compilability, one could either require excep clauses to
be before the beginning of the compound statement they modify
or at the beginning of the compound statement that they modify.
I don't know which would be less insulting to C.



More information about the Cfp-interest mailing list