Topic: Why does "try" require a compound statement to follow it?


Author: Martin Fabian <fabianNOSPAM@s2.chalmers.se>
Date: 1999/10/26
Raw View
"M. Leene" wrote:
>
> Richard Burkert <rrburkert@hotmail.com> wrote in message
> news:7tgjbj$42h$1@newsmonger.rutgers.edu...
> > My compiler told me recently that try requires a compound statement block,
> > rather than allowing the programmer to choose between a simple statement or
> > a compound one.  I wonder why this is, because it seems to go against the
> > convention of most other C++ statements.
>
> Yes I wonder too, let's wait for Bjarne ...
>
Well, Stroustrup himself says in the D&E (16.3) that "the try keyword
is completely redundant and so are the { } brackets except where
multiple statements are actually used in a try-block or a handler."

--
Martin Fabian                         http://www.s2.chalmers.se/~fabian/
                                                                      --
"Cheer up. It may never happen"                          (Edina Monsoon)

        /* Remove NOSPAM from reply-to address to mail me */
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/10/21
Raw View
In article <939632169.652211@tbird.introweb.nl>, M. Leene
<mckmk98NO_SPAM_@yahoo.com> writes
>
>Richard Burkert <rrburkert@hotmail.com> wrote in message
>news:7tgjbj$42h$1@newsmonger.rutgers.edu...
>> My compiler told me recently that try requires a compound statement block,
>> rather than allowing the programmer to choose between a simple statement
>or
>> a compound one.  I wonder why this is, because it seems to go against the
>> convention of most other C++ statements.
>>
>Yes I wonder too, let's wait for Bjarne ...
>
>But, maybe it has to do with some 'compatibility' with the macro-implemented

Actually there is quite a lot of syntactic sugar in C++ exception
syntax.  You actually do not need the keyword try but it increased
programmers comfort factor so it is there (and required).  I suspect
that the use of 'compound statements' is similar.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 1999/10/21
Raw View
Andrew Koenig wrote:
>
> Richard Burkert <rrburkert@hotmail.com> wrote:
>
>> But that's when the compiler gave me the error about try requiring a
>> compound statement block.  Anyone have an explanation for this?
>
> The idea is to avoid the ambiguity that would otherwise arise in
> cases such as
>
>    try try f(); catch (e1) { } catch (e2) { } catch (e3) { }
>
> Without the requirement that "try" be followed by a compound
> statement, there is no way to tell which catch clauses go with which
> try blocks.

I would expect it to bind with the most previous 'try', a la 'if/else':

  if (t1) if (t2) f(); else if (e1) {} else if (e2) {} else if (e3) {}

After all, a series of catch blocks is semantically similar to a
series of else-ifs (with a final 'catch(...)' acting like a final
'else').

Or, to make it more visual:

    try
        try
            f();
        catch (e1)
        {
        }
        catch (e2)
        {
        }
        catch (e3)
        {
        }

...which is missing a catch for the first try.

But there is no compelling reason why a brace-enclosed block is
necessary for try/catch statement syntax, other than it makes it
less likely to code nested try/catch blocks incorrectly (and how
common are those?).

If the syntax was relaxed a little, "simple" try/catch groups would
be legal:

    try
        Foo::action();
    catch (Foo::Exception e)
        cleanup(e);
    catch (...)
        giveUp();

-- David R. Tribble, david@tribble.com, http://www.david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Phil Carmody <carmody@cpd.ntc.nokia.com>
Date: 1999/10/21
Raw View
Andrew Koenig wrote:
>
> In article <7tgjbj$42h$1@newsmonger.rutgers.edu>,
> Richard Burkert <rrburkert@hotmail.com> wrote:
>
> >But that's when the compiler gave me the error about try requiring a
> >compound statement block.  Anyone have an explanation for this?  Thanks.
>
> The idea is to avoid the ambiguity that would otherwise arise in
> cases such as
>
>         try try f(); catch (e1) { } catch (e2) { } catch (e3) { }
>
> Without the requirement that "try" be followed by a compound statement,
> there is no way to tell which catch clauses go with which try blocks.

Don't 'tell' which, in that case; 'assert' which instead.

I can't claim to be a formal grammer expert, but is your example really
more complicated than the situation before the C grammer was formalised
and
expressions such as

if(cond1)
  if(cond2)
     statement(1);
else
  statement(2);

or

cond1 ? cond2 ? expression1 : expression2 : expression3;
cond1 ? cond2 : cond3 ? expression1 : expression2;

or

lvalue = value1 = value2;

or

int InClosureOf1stQuadrant = positive_p(x) || zero_p(x) &&
                             positive_p(y) || zero_p(y);

were considered.

Decisions were made about how things should associate etc, and I assume
in most cases that which was chosen was "that which seems most obvious
or has the easiest grammer"

Could the grammer not have asserted that the catches always correcpond
to
the innermost try (innermost such that there are enough catches left for
the outer ones)?
Could it instead not have asserted that the final catch block should be
followed by a semicolon?

I'm convinced that there is always more than one way to do things, and
that there exist similar grammers which assert that try need not take a
block.

I'm no saying it's wrong, I'm trying to be completely disinterested.
(If anything I prefer blocks, but that's irrelevant...)

Phil
--
Phil Carmody
Not speaking for or on behalf of Nokia Telecommunications

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: icedancer-zamboni@ibm-zamboni.net (Ken Walter)
Date: 1999/10/14
Raw View
On Tue, 12 Oct 1999 21:02:59, "M. Leene" <mckmk98NO_SPAM_@yahoo.com>
wrote:

>
>Richard Burkert <rrburkert@hotmail.com> wrote in message
>news:7tgjbj$42h$1@newsmonger.rutgers.edu...
>> My compiler told me recently that try requires a compound statement block,
>> rather than allowing the programmer to choose between a simple statement
>or
>> a compound one.  I wonder why this is, because it seems to go against the
>> convention of most other C++ statements.
>>
>Yes I wonder too, let's wait for Bjarne ...
>
>But, maybe it has to do with some 'compatibility' with the macro-implemented
>versions of exceptions by compilervendors before this was in the standard.
>
>ML
>
I would guess that the implementation needed a scope
to take care of constructing and destructing exception objects
plus any other objects in the "statement".

Ken Walter

Remove -zamboni to reply
All the above is hearsay and the opinion of no one in particular

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: ark@research.att.com (Andrew Koenig)
Date: 1999/10/15
Raw View
In article <7tgjbj$42h$1@newsmonger.rutgers.edu>,
Richard Burkert <rrburkert@hotmail.com> wrote:

>But that's when the compiler gave me the error about try requiring a
>compound statement block.  Anyone have an explanation for this?  Thanks.

The idea is to avoid the ambiguity that would otherwise arise in
cases such as

 try try f(); catch (e1) { } catch (e2) { } catch (e3) { }

Without the requirement that "try" be followed by a compound statement,
there is no way to tell which catch clauses go with which try blocks.
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/10/15
Raw View
Steve Clamage wrote:

> No, it is a syntax issue. Suppose braces were not required:
>
> try
>     try
>         foo();
>     catch(T t) { ... }
> catch(U u) { ... }
> ...
>
> A "catch" must be associated with some "try". In my example, I want
> the second catch to be associated with the first try, as indicated
> by the indentation. But since multiple catch blocks are allowed for
> a try, the compiler would have to associate every catch with the
> nearest try.
>
> Instead of having special disambiguating rules as with if-else,

And it's _much_ worse than else, as there is no

if (x)
   instr1
else
   instr2
else
   instr3

construct in C++, but you have the equivalent w/ exceptions.

--

Valentin Bonnard
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "M. Leene" <mckmk98NO_SPAM_@yahoo.com>
Date: 1999/10/12
Raw View
Richard Burkert <rrburkert@hotmail.com> wrote in message
news:7tgjbj$42h$1@newsmonger.rutgers.edu...
> My compiler told me recently that try requires a compound statement block,
> rather than allowing the programmer to choose between a simple statement
or
> a compound one.  I wonder why this is, because it seems to go against the
> convention of most other C++ statements.
>
Yes I wonder too, let's wait for Bjarne ...

But, maybe it has to do with some 'compatibility' with the macro-implemented
versions of exceptions by compilervendors before this was in the standard.

ML



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]







Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/13
Raw View
"M. Leene" <mckmk98NO_SPAM_@yahoo.com> writes:

>Richard Burkert <rrburkert@hotmail.com> wrote in message
>news:7tgjbj$42h$1@newsmonger.rutgers.edu...
>> My compiler told me recently that try requires a compound statement block,
>> rather than allowing the programmer to choose between a simple statement or
>> a compound one.  I wonder why this is, because it seems to go against the
>> convention of most other C++ statements.
>>
>Yes I wonder too, let's wait for Bjarne ...

>But, maybe it has to do with some 'compatibility' with the macro-implemented
>versions of exceptions by compilervendors before this was in the standard.

No, it is a syntax issue. Suppose braces were not required:

try
    try
 foo();
    catch(T t) { ... }
catch(U u) { ... }
...

A "catch" must be associated with some "try". In my example, I want
the second catch to be associated with the first try, as indicated
by the indentation. But since multiple catch blocks are allowed for
a try, the compiler would have to associate every catch with the
nearest try.

Instead of having special disambiguating rules as with if-else, C++
uses the simpler rule of requiring full sets of braces for try-catch.
There is never any ambiguity, and you won't be mislead by
indentation. Just follow the braces.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James.Kanze@dresdner-bank.com
Date: 1999/10/13
Raw View
In article <939632169.652211@tbird.introweb.nl>,
  "M. Leene" <mckmk98NO_SPAM_@yahoo.com> wrote:
>
> Richard Burkert <rrburkert@hotmail.com> wrote in message
> news:7tgjbj$42h$1@newsmonger.rutgers.edu...

> > My compiler told me recently that try requires a compound statement
> > block, rather than allowing the programmer to choose between a
> > simple statement or a compound one.  I wonder why this is, because
> > it seems to go against the convention of most other C++ statements.

> Yes I wonder too, let's wait for Bjarne ...

> But, maybe it has to do with some 'compatibility' with the
> macro-implemented versions of exceptions by compilervendors before
> this was in the standard.

Or maybe it has to do with not repeating past errors.

--
James Kanze                    mailto:James.Kanze@dresdner-bank.com
Conseils en informatique orient   e objet/
                  Beratung in objekt orientierter Datenverarbeitung
Ziegelh   ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627


Sent via Deja.com http://www.deja.com/
Before you buy.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/10/14
Raw View
Richard Burkert wrote:
>
> My compiler told me recently that try requires a compound statement block,
> rather than allowing the programmer to choose between a simple statement or
> a compound one.  I wonder why this is, because it seems to go against the
> convention of most other C++ statements.
>

The D&E says that the syntax for exception handling was designed so
verbose to "save support staff from confused users". We should call the
AT&T C++ support :-)

I am also wondering whether this issue came up in the standardization
process. It appears that a trivial enhancement to the grammar will allow
simple statement to be allowed as a try 'block'.

--
Biju Thomas


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Richard Burkert" <rrburkert@hotmail.com>
Date: 1999/10/11
Raw View
My compiler told me recently that try requires a compound statement block,
rather than allowing the programmer to choose between a simple statement or
a compound one.  I wonder why this is, because it seems to go against the
convention of most other C++ statements.

This arose because I had a try clause that only enclosed a while loop, and I
thought I'd save my code from being tabbed in one more time, so I changed:
try {
    while {
        /* sad code, indented more than should be necessary */
    }
}
to:
try while {
    /* happy code */
}

But that's when the compiler gave me the error about try requiring a
compound statement block.  Anyone have an explanation for this?  Thanks.

Rich




      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]