This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

function try block bug


A's constructor throws an exception which is not caught by B's
constructor function try block. According to the standard (december
working paper) the statement after ////////!!!!!!!!!!! should be
executed. The output is shown at the end.

================================================
===================== code =====================
================================================
#include <iostream.h>
#include <stdexcept>

// A 
class A {
public:
  A(int const i);
  A(A const &a)  : i_(a.i_) { cerr << "In A copy constructor\n"; };
  ~A() { cerr << "In A destructor\n";}
private:
  int i_;
};
A::A(int const i) : i_(i) { 
  cerr << "In A constructor\n"; 
  if(i == 10){ 
    cerr << "A constructor throws a logic_error\n";
    throw(logic_error("Exception thrown by A's constructor")); 
  }
}
// B 
class B {
public:
  B(int j); 
  ~B() { cerr << "In B destructor\n"; }
private:
  int j_;
  A   A_;
};
/////////////////////////////////////// Interesting part
B::B(int j) try : j_(j) , A_(j-10) {
  cerr << "In B constructor\n";
}catch(...){
///////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  cerr << "Good!\n";throw; 
}

// main 
int
main(){
  try{

    B(20);
    
  }catch(exception &E){
    cerr << "Exception caught\n";
    cerr << E.what() << endl;
  }
}
===============================================
===================== execution ===============
===============================================
toucan:~/work/C++> uname -a
Linux toucan.physics.utoronto.ca 2.0.36 #3 Sat Jan 9 19:07:50 EST 1999
i686 unknown
toucan:~/work/C++> g++ -v
Reading specs from
/usr/local/Packages/egcs/install/lib/gcc-lib/i686-pc-linux-gnu/egcs-2.93.04/specs
gcc version egcs-2.93.04 19990131 (gcc2 ss-980929 experimental)
toucan:~/work/C++> g++ -ansi -W -Wall  -mpentiumpro -pipe -o exe main.cc
toucan:~/work/C++> ldd exe
        libstdc++-libc6.0-1.so.2 =>
/usr/local/Packages/egcs/install/lib/libstdc++-libc6.0-1.so.2
(0x40004000)
        libm.so.6 => /lib/libm.so.6 (0x40045000)
        libc.so.6 => /lib/libc.so.6 (0x40060000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00000000)
toucan:~/work/C++> exe
In A constructor
A constructor throws a logic_error
In A destructor
Exception caught
Exception thrown by A's constructor

-- 
        Ovidiu


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]