Topic: Extending exceptions of a base class's method ?


Author: Genty Jean-Paul <gentyjp@libertysurf.fr>
Date: Wed, 26 Sep 2001 17:08:18 GMT
Raw View
 Hi

 I have a question about adding some exceptions to the implementation
of a virtual method of a base class.

class Base{
void Action(void) throw(int,const char*) = 0;
};

class Concrete : public Base{
  void Action(void) throw(char){
   // just a test
   char c = 'a';
   throw(c);
  }
};

void MyTest(void)
{
 Concrete w;
 try{
 w.action()
  }
  catch(char &c)
  {
     cout << " char exception : " << c <<endl;
  }
}

Can I do it ?
Does all compilers react the same ?

( I tested it on gcc 2.95.3 and VC++6/SP5 and it worked)

Thanks for your answers.

Jean-Paul Genty

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Attila Feher <Attila.Feher@lmf.ericsson.se>
Date: Wed, 26 Sep 2001 17:54:23 GMT
Raw View
Genty Jean-Paul wrote:
>
> class Base{
> void Action(void) throw(int,const char*) = 0;
> };
>
> class Concrete : public Base{
>   void Action(void) throw(char){
[SNIP]
> Can I do it ?

I dunno, but you better not do it.

> Does all compilers react the same ?

Maybe.  But the trouble is:

1) You do not _extend_ the specification, you _change_ it.  You remove
int and const char * and add char instead.

2) I see trouble with it even if you extend.  Why.  The exception
specification is part of the "agreement", the polymorph interface.  So X
writes a function (worst case a destructor) in which he uses Base * or
Base &.  He looks at the interface and says: OK, I need to catch int and
const char * here to ensure no exception escapes from my constructor.
Ten you throw a char...  Not nice.

Attila

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 26 Sep 2001 19:05:23 GMT
Raw View
In article <gr33rt8lrujeiler7fi4mq6tdp8vt4p6h6@4ax.com>, Genty Jean-Paul
<gentyjp@libertysurf.fr> writes
>
> Hi
>
> I have a question about adding some exceptions to the implementation
>of a virtual method of a base class.
>
>class Base{
>void Action(void) throw(int,const char*) = 0;
>};

You do need to specify that Action is virtual else your code will not
compile.  BTW, you do not need that second use of 'void', it is only
meaningful in C.


>
>class Concrete : public Base{
>  void Action(void) throw(char){

The compiler should give you an error because this exception spec is not
a subset of the one in the base, however I suspect it may sneak by some
compilers because char is implicitly convertible to an int.  It
shouldn't, IMO, because you cannot catch a char by int&, only by int
const & (or by value)

Note that the rules would be different if instead of int and char you
had base and derived as exception types, because (if correctly designed)
derived would be a subtype of base and so substitutable. The compiler
will assume that this is true if you use public inheritance.


>   // just a test
>   char c = 'a';
>   throw(c);
>  }
>};
>
>void MyTest(void)
>{
> Concrete w;
> try{
>       w.action()
>  }
>  catch(char &c)
>  {
>     cout << " char exception : " << c <<endl;
>  }
>}
>
>Can I do it ?
>Does all compilers react the same ?

I think the compiler is being overly liberal in interpreting the
requirements of the Standard.



Francis Glassborow
I offer my sympathy and prayers to all those who are suffering
as a result of the events of September 11 2001.

---
[ 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.research.att.com/~austern/csc/faq.html                ]