Topic: Derived catch clauses preceding base catch clauses


Author: richard@ex-parrot.com (Richard Smith)
Date: Thu, 20 Nov 2003 03:19:23 +0000 (UTC)
Raw View
James Kuyper wrote:
> 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                       ]





Author: ahp6@email.byu.edu ("Adam H. Peterson")
Date: Thu, 20 Nov 2003 22:54:37 +0000 (UTC)
Raw View
>>
>>  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                       ]





Author: Usenet@aristeia.com (Scott Meyers)
Date: Mon, 17 Nov 2003 22:34:49 +0000 (UTC)
Raw View
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                       ]





Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Wed, 19 Nov 2003 00:26:49 +0000 (UTC)
Raw View
Scott Meyers wrote:
> 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                       ]





Author: richard@ex-parrot.com (Richard Smith)
Date: Wed, 19 Nov 2003 00:27:10 +0000 (UTC)
Raw View
Scott Meyers wrote:

> 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                       ]





Author: kuyper@wizard.net (James Kuyper)
Date: Wed, 19 Nov 2003 16:25:57 +0000 (UTC)
Raw View
richard@ex-parrot.com (Richard Smith) wrote in message news:<Pine.LNX.4.55.0311181738000.27380@sphinx.mythic-beasts.com>...
> 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                       ]