 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gregg Guest
|
Posted: Thu Mar 04, 2004 10:51 pm Post subject: try / catch syntax |
|
|
What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements (possibly compound)? It seems
inconsistent with other similar constructs such as if / else. Why not
allow
try
<stmt1>
catch (std::exception&)
<stmt2>
where <stmt2> and <stmt2> could be either simple or compound (with
braces) exactly as if / else?
Thanks
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ray Lischner Guest
|
Posted: Fri Mar 05, 2004 4:05 am Post subject: Re: try / catch syntax |
|
|
On Thursday 04 March 2004 05:51 pm, Gregg wrote:
| Quote: | What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements (possibly compound)?
|
You need the braces to tell which catch goes with which try.
For example:
try
try
try_this();
catch (exception1&)
handle1();
catch (exception2&)
handle2();
catch (exception3&)
handle3();
--
Ray Lischner, author of C++ in a Nutshell
http://www.tempest-sw.com/cpp
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Fri Mar 05, 2004 3:04 pm Post subject: Re: try / catch syntax |
|
|
[email]rl.news (AT) tempest-sw (DOT) com[/email] (Ray Lischner) wrote in message news:<1785.4047f27d.858ca (AT) prospero (DOT) island.local>...
| Quote: | On Thursday 04 March 2004 05:51 pm, Gregg wrote:
What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements (possibly compound)?
You need the braces to tell which catch goes with which try.
For example:
try
try
try_this();
catch (exception1&)
handle1();
catch (exception2&)
handle2();
catch (exception3&)
handle3();
|
I'm not convinced this is the only reason, because it would have been
just as reasonable to stipulate that catch'es go with the closest try,
as it happens with else's and if's. If this were the case, the example
above would result in a "try without catch" error.
Cheers,
Nicola Musatti
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Joe Greer Guest
|
Posted: Fri Mar 05, 2004 3:04 pm Post subject: Re: try / catch syntax |
|
|
[email]rl.news (AT) tempest-sw (DOT) com[/email] (Ray Lischner) wrote in message news:<1785.4047f27d.858ca (AT) prospero (DOT) island.local>...
| Quote: | On Thursday 04 March 2004 05:51 pm, Gregg wrote:
What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements (possibly compound)?
You need the braces to tell which catch goes with which try.
For example:
try
try
try_this();
catch (exception1&)
handle1();
catch (exception2&)
handle2();
catch (exception3&)
handle3();
|
I personally don't find this a compelling argument. You run into a
similar situation with if statements:
if (this)
if (that)
else
....
and one just knows that if you are going to nest, then you must use
braces. The same rules could apply here. I am not really advocating
a change, but there really should be a better rationale than that.
joe
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ray Lischner Guest
|
Posted: Fri Mar 05, 2004 3:35 pm Post subject: Re: try / catch syntax |
|
|
On Friday 05 March 2004 10:04 am, Nicola Musatti wrote:
| Quote: | I'm not convinced this is the only reason, because it would have been
just as reasonable to stipulate that catch'es go with the closest try,
as it happens with else's and if's. If this were the case, the example
above would result in a "try without catch" error.
|
The difference is that try can have mutliple catch parts. An if
statement can have only one else part.
--
Ray Lischner, author of C++ in a Nutshell
http://www.tempest-sw.com/cpp
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
P.J. Plauger Guest
|
Posted: Fri Mar 05, 2004 7:55 pm Post subject: Re: try / catch syntax |
|
|
"Ray Lischner" <rl.news (AT) tempest-sw (DOT) com> wrote
| Quote: | On Friday 05 March 2004 10:04 am, Nicola Musatti wrote:
I'm not convinced this is the only reason, because it would have been
just as reasonable to stipulate that catch'es go with the closest try,
as it happens with else's and if's. If this were the case, the example
above would result in a "try without catch" error.
The difference is that try can have mutliple catch parts. An if
statement can have only one else part.
|
True, but there *still* could be a simple disambiguation rule
for catch clauses. Each could bind to the immediately preceding
try clause. But the whole discussion is academic because that's
now the grammar that was chosen, for whatever reason.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Tony Burrows Guest
|
Posted: Sat Mar 06, 2004 4:38 am Post subject: Re: try / catch syntax |
|
|
Slightly differently
Why no finally which gets executed whether try or catch happens. Ie, code
that always runs before a function ends?
As a Java programmer I find it useful at times.
Tony
On Fri, 05 Mar 2004
15:35:01 +0000, Ray Lischner wrote:
| Quote: | On Friday 05 March 2004 10:04 am, Nicola Musatti wrote:
I'm not convinced this is the only reason, because it would have been
just as reasonable to stipulate that catch'es go with the closest try,
as it happens with else's and if's. If this were the case, the example
above would result in a "try without catch" error.
The difference is that try can have mutliple catch parts. An if
statement can have only one else part.
|
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Gregg Guest
|
Posted: Sat Mar 06, 2004 4:38 am Post subject: Re: try / catch syntax |
|
|
[email]rl.news (AT) tempest-sw (DOT) com[/email] (Ray Lischner) wrote in message news:<1785.4047f27d.858ca (AT) prospero (DOT) island.local>...
| Quote: | You need the braces to tell which catch goes with which try.
|
I hadn't considered the case of nested handlers with multipe catch
clauses, and it seems obvious now that that was the reason for
requiring the braces. However, it still strikes me as an ugly
linguistic inconsistency. It seems that it would have been better to
apply rules similar to those for disambiguating nested if/else
statements, extended to handle mulitple catch clauses.
Gregg
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Clem Dickey Guest
|
Posted: Sat Mar 06, 2004 4:46 pm Post subject: Re: try / catch syntax |
|
|
[email]gregglastname (AT) hotmail (DOT) com[/email] (Gregg) wrote in message news:<20d0a2b2.0403041413.13e57e69 (AT) posting (DOT) google.com>...
| Quote: | What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements
|
Perhaps blocks are required to avoid a gratuitous difference with
function definitions ;-)
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Ken Hagan Guest
|
Posted: Sun Mar 07, 2004 6:06 am Post subject: Re: try / catch syntax |
|
|
It was an arbitrary decision. Bjarne considered allowing the braces
to be absent but decided it would be safer to insist on them. (I
think this is mentioned in "The Design and Evolution of C++", along
with the point that, strictly speaking, the "try" is redundant too.)
As for why the standard kept the rule rather than making it
consistent with other parts of the language, I can only guess,
but I would guess that the suggestion never came up. "try" and
"catch" are suffiently rare in real programs that it probably
doesn't matter.
"Gregg" <gregglastname (AT) hotmail (DOT) com> wrote...
| Quote: | What was the rationale behind requiring the bodies of try and catch to
be blocks instead of statements (possibly compound)? It seems
inconsistent with other similar constructs such as if / else.
|
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Edward Diener Guest
|
Posted: Sun Mar 07, 2004 7:10 am Post subject: Re: try / catch syntax |
|
|
Tony Burrows wrote:
| Quote: | Slightly differently
Why no finally which gets executed whether try or catch happens. Ie,
code that always runs before a function ends?
As a Java programmer I find it useful at times.
|
Java needs it because it has no deterministic destruction of objects. Java
programmers have to make sure they manually release resources whereas C++
programmers don't have to worry about this at all. That is why a 'finally'
has much less importance in C++ than it does in Java ( or C# ). So C++ never
adopted such a measure, and I don't believe it needs it. A number of C++
compiler implementations have added it as an extension to C++.
| Quote: |
Tony
On Fri, 05 Mar 2004
15:35:01 +0000, Ray Lischner wrote:
On Friday 05 March 2004 10:04 am, Nicola Musatti wrote:
I'm not convinced this is the only reason, because it would have
been just as reasonable to stipulate that catch'es go with the
closest try, as it happens with else's and if's. If this were the
case, the example above would result in a "try without catch" error.
The difference is that try can have mutliple catch parts. An if
statement can have only one else part.
|
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Andrew Koenig Guest
|
Posted: Sun Mar 07, 2004 7:10 am Post subject: Re: try / catch syntax |
|
|
""P.J. Plauger"" <pjp (AT) dinkumware (DOT) com> wrote
| Quote: | True, but there *still* could be a simple disambiguation rule
for catch clauses. Each could bind to the immediately preceding
try clause.
|
Unfortunately, that rule does the wrong thing for the following case:
try f(); try g(); catch(...) x(); catch(...) y();
Because each try must have at least one catch, there is only one sensible
way to parse this example: The first catch must go with the second try, and
the second catch must go with the first try. If each catch binds to the
immediately preceding try, then the first try winds up without a
corresponding catch.
So I think a disambiguation rule would have to be more complicated to be
useful.
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
P.J. Plauger Guest
|
Posted: Sun Mar 07, 2004 5:20 pm Post subject: Re: try / catch syntax |
|
|
""Andrew Koenig"" <ark (AT) acm (DOT) org> wrote
| Quote: | ""P.J. Plauger"" <pjp (AT) dinkumware (DOT) com> wrote in message
news:RO22c.65045$C65.38591 (AT) nwrddc01 (DOT) gnilink.net...
True, but there *still* could be a simple disambiguation rule
for catch clauses. Each could bind to the immediately preceding
try clause.
Unfortunately, that rule does the wrong thing for the following case:
try f(); try g(); catch(...) x(); catch(...) y();
Because each try must have at least one catch, there is only one sensible
way to parse this example: The first catch must go with the second try,
and
the second catch must go with the first try. If each catch binds to the
immediately preceding try, then the first try winds up without a
corresponding catch.
So I think a disambiguation rule would have to be more complicated to be
useful.
|
Agreed, but there are other simple rules in C/C++ that don't always give
the "useful" result, such as x+++++y. Yet we've kept them for simplicity,
in some dimension.
This quibble doesn't alter the fact that it was probably wise to require
compound statements after try and catch.
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Sun Mar 07, 2004 5:20 pm Post subject: Re: try / catch syntax |
|
|
[email]tony (AT) tonyburrows (DOT) com[/email] (Tony Burrows) wrote in message news:<pan.2004.03.05.17.10.05.427664 (AT) tonyburrows (DOT) com>...
| Quote: | Slightly differently
Why no finally which gets executed whether try or catch happens. Ie, code
that always runs before a function ends?
As a Java programmer I find it useful at times.
Tony
|
Because in C++, stack based objects get destroyed deterministically.
Whatever you would put into a finally block, just put into the
destructor of a stack based object.
Now, you might say that writing stack based objects just to have
specialized destructors is a pain, and you'd be correct. Enter
ScopeGuard, C++'s finally!
http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/alexandr.htm
http://tinyurl.com/26vzp
ScopeGuard lets you create stack-based object's, on the fly, who's
destructor does whatever you want:
FILE *pFile = FOPEN(...);
ON_BLOCK_EXIT(fclose,pFile);
-joshua lehrer
factset research systems
NYSE:FDS
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Craig Henderson Guest
|
Posted: Sun Mar 07, 2004 6:03 pm Post subject: Re: try / catch syntax |
|
|
| Quote: | Why not allow
try
stmt1
catch (std::exception&)
stmt2
where <stmt2> and <stmt2> could be either simple or compound (with
braces) exactly as if / else?
|
There can be many catch() statements associated with each try:
try {
} catch (std::exception const &) {
} catch (...) {
}
and trys can be nested
try {
try {
} catch (std::exception const &) {
// handle known exception and absorb it
} catch (...) {
// report unknown exception and rethrow
...
throw;
}
} catch (...) {
// handle the unknown exception at an outer scope
}
Now, if the curlies were removed, this becomes
try // (1)
try // (2)
<stmt1>
catch (std::exception const &)
<stmt2>
catch (...) // (3)
<stmt3>
catch (...) // (4) -- ambigous
<stmt4>
The catch(...) at (4) has lost context, and will look as though it belongs
to try (2), but has already been defined at (3). This is different to the
if..else example you cited, as each 'if' can have zero or one else clauses,
but try can have one or more catch-s.
-- Craig
---
[ 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://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|