RE: [gtkmm] OT: C++ exceptions



> -----Original Message-----
> From: gtkmm-list-admin gnome org [mailto:gtkmm-list-admin gnome org]On
> Behalf Of Spandan Bhatt
> Sent: quinta-feira, 11 de Julho de 2002 4:31
> To: Jarek Dukat; Gtkmm
> Subject: Re: [gtkmm] OT: C++ exceptions
> 
> 
> Can somebody enlighten me?
> How is unexpected exception thrown by this code?
> what does
> Foo(int _i) throw(Ex1);
> mean?(does it always throw the exception? Even if it
> does from the code I saw it looked like that exception
> was suposed to be handled!! where is the unexpected
> exception. I must be making some silly mistake..)
> Spundun

Foo(int _i) throw(Ex1);

means that you are asserting that the only exception
that Foo() is able to throw is Ex1. If the function happens
to throw any other exception, the default behaviour is to
call unexpected(), which will call terminate() and thus
ending your program.

Which is right for the program below. The function says
that it will only throw Ex1 but it throws Ex2 that isn't
a derived class of Ex1, so it is an *unexpected* exception.


As for VC++ not calling unexpected() that is usual behaviour
for that compiler. As usual, M$ lack of adherence to standards
is well known. See this month CUJ for an article about that.

Paulo Pinto

> --- Jarek Dukat <madmaxer poczta fm> wrote:
> > This question is OT, but there are many C++ experts
> > here so perhaps you
> > could explain it. Please look at this sample code:
> > 
> > // ---------------------------------------------
> > #include <exception>
> > #include <iostream>
> > using namespace std;
> > 
> > struct Ex1 {};
> > struct Ex2 {};
> > 
> > class Foo {
> >   int i;
> > public:
> >   Foo(int _i) throw(Ex1);
> >   ~Foo();
> > };
> > Foo::Foo(int _i) throw(Ex1) : i(_i) {
> >   cerr << "Foo CTOR: " << i << endl;
> >   if ( i == 2 )
> >     throw Ex2();
> > }
> > Foo::~Foo() {
> >   cerr << "Foo DTOR: " << i << endl;
> > }
> > 
> > void my_unex() {
> >   cerr << "UNEXPECTED!" << endl;
> >   throw;
> > }
> > 
> > int main() {
> >   set_unexpected(my_unex);
> >   try {
> >     Foo foo1(1);
> >     Foo foo2(2);
> >   } catch ( Ex1& ) {
> >     cerr << "Ex1 caught" << endl;
> >   } catch ( Ex2& ) {
> >     cerr << "Ex2 caught" << endl;
> >   }
> >   return 0;
> > }
> > // ---------------------------------------------
> > 
> > After compiling it with different compilers I get
> > different! results!
> > 
> > --- VC6:
> > Foo CTOR: 1
> > Foo CTOR: 2
> > Foo DTOR: 1
> > Ex2 caught
> > 
> > --- G++ 3.1 (dla 2.95 tak samo):
> > Foo CTOR: 1
> > Foo CTOR: 2
> > UNEXPECTED!
> > Aborted
> > 
> > And according to my knowledge about C++ exceptions a
> > correct result
> > should be:
> > Foo CTOR: 1
> > Foo CTOR: 2
> > UNEXPECTED!
> > Foo DTOR: 1
> > Ex2 caught
> > 
> > Obviously VC ignores unexpected() handler, and G++
> > treats it much too
> > seriously :)
> > What do you think about it?
> > 
> > -- 
> > struct Sig {
> >   string name("     J a r e k   D u k a t     ");
> >   string mail(" madmaxer (at) poczta (dot) fm ");
> > };
> > 
> > 
> >
> ----------------------------------------------------------------------
> > Wiesz, co zdarzylo sie dzisiaj? >>>
> > http://link.interia.pl/f1605
> > 
> > 
> > _______________________________________________
> > gtkmm-list mailing list
> > gtkmm-list gnome org
> > http://mail.gnome.org/mailman/listinfo/gtkmm-list
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Sign up for SBC Yahoo! Dial - First Month Free
> http://sbc.yahoo.com
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtkmm-list
> 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]