[gtkmm] OT: C++ exceptions
- From: Jarek Dukat <madmaxer poczta fm>
- To: Gtkmm <gtkmm-list gnome org>
- Subject: [gtkmm] OT: C++ exceptions
- Date: 07 Jul 2002 12:14:05 +0200
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
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]