 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Scott Meyers Guest
|
Posted: Mon Nov 17, 2003 10:34 pm Post subject: Derived catch clauses preceding base catch clauses |
|
|
Recently I was reminded of something in C++ that I just don't understand.
If a base class catch clause precedes a derived class catch clause, the
second clause can never be executed:
struct B { ... };
struct D: B { ... };
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
As I recall, this was disallowed in the ARM, but the rule was changed for
the final standard. Can somebody please explain to me why it's not an
error for a base catch clause to precede a derived catch clause? If it's
an issue of accessibility (e.g., it might make sense if D privately
inherits from B), then why not prohibit it when D can be implicitly
converted to B?
I'm sure there's a good reason for the current rules. I'd just like to
know what it is.
Thanks,
Scott
---
[ 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 |
|
 |
Sergey P. Derevyago Guest
|
Posted: Wed Nov 19, 2003 12:26 am Post subject: Re: Derived catch clauses preceding base catch clauses |
|
|
Scott Meyers wrote:
| Quote: | Recently I was reminded of something in C++ that I just don't understand.
If a base class catch clause precedes a derived class catch clause, the
second clause can never be executed:
struct B { ... };
struct D: B { ... };
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
As I recall, this was disallowed in the ARM, but the rule was changed for
the final standard. Can somebody please explain to me why it's not an
error for a base catch clause to precede a derived catch clause? If it's
an issue of accessibility (e.g., it might make sense if D privately
inherits from B), then why not prohibit it when D can be implicitly
converted to B?
I'm sure there's a good reason for the current rules. I'd just like to
know what it is.
IMHO 308 "Catching exceptions with ambiguous base classes" NAD issue gives |
some points.
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_closed.html#308
--
With all respect, Sergey. http://cpp3.virtualave.net/
mailto : ders at skeptik.net
---
[ 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 |
|
 |
Richard Smith Guest
|
Posted: Wed Nov 19, 2003 12:27 am Post subject: Re: Derived catch clauses preceding base catch clauses |
|
|
Scott Meyers wrote:
| Quote: | Recently I was reminded of something in C++ that I just don't understand.
If a base class catch clause precedes a derived class catch clause, the
second clause can never be executed:
struct B { ... };
struct D: B { ... };
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
|
[BTW: These two semicolons are illegal.]
It *is* possible for the catch block for D to be entered.
If B is an ambiguous public base class of the exception, but
D is not, then the second catch block will be entered. For
example, Imagine the code within the try block throws an
object of type E, defined below.
struct E : D, B {};
--
Richard Smith
---
[ 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 |
|
 |
James Kuyper Guest
|
Posted: Wed Nov 19, 2003 4:25 pm Post subject: Re: Derived catch clauses preceding base catch clauses |
|
|
[email]richard (AT) ex-parrot (DOT) com[/email] (Richard Smith) wrote in message news:<Pine.LNX.4.55.0311181738000.27380 (AT) sphinx (DOT) mythic-beasts.com>...
| Quote: | Scott Meyers wrote:
....
struct B { ... };
struct D: B { ... };
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
[BTW: These two semicolons are illegal.]
|
Not quite - they both are quite legal, though pointless, since they
each terminate empty statements that follow the preceeding catch block
(which is probably NOT what the author intended them to represent).
The first ';' renders the following 'catch' illegal, since there's no
corresponding 'try' for it; it's no long part of the same try-block as
the first catch. The second semicolon terminates a perfectly legal
empty statement. But both semicolons are themselves legal.
---
[ 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 |
|
 |
Richard Smith Guest
|
Posted: Thu Nov 20, 2003 3:19 am Post subject: Re: Derived catch clauses preceding base catch clauses |
|
|
James Kuyper wrote:
| Quote: | Richard Smith wrote:
Scott Meyers wrote:
....
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
[BTW: These two semicolons are illegal.]
Not quite - they both are quite legal, though pointless, since they
each terminate empty statements that follow the preceeding catch block
(which is probably NOT what the author intended them to represent).
The first ';' renders the following 'catch' illegal, since there's no
corresponding 'try' for it; it's no long part of the same try-block as
the first catch. The second semicolon terminates a perfectly legal
empty statement. But both semicolons are themselves legal.
|
It's a bit pointless arguing whether the semicolons
themselves are legal or not if the it makes the code as a
whole ill-formed. The Standard does not have any notion of
whether a code fragment, such as an semicolon in isolation,
is well-formed: it only has the notion of whether a whole
translation unit is well-formed. Unless the preprocessor
is used to change and/or hide the code, the first semicolon
prevents the code _as a whole_ from being legal in any
context. Given that, I think it's reasonable to say the
first semicolon is illegal.
The second semicolon is perfectly OK -- my mistake. Sorry.
--
Richard Smith
---
[ 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 |
|
 |
Adam H. Peterson Guest
|
Posted: Thu Nov 20, 2003 10:54 pm Post subject: Re: Derived catch clauses preceding base catch clauses |
|
|
| Quote: |
struct B { ... };
struct D: B { ... };
try {
...
}
catch(B) { ... };
catch(D) { ... }; // can never be executed
[BTW: These two semicolons are illegal.]
|
Actually, I believe only the first one is . (The second one is
useless, but not illegal.)
Adam H. Peterson
---
[ 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
|
|