Thread: Exception problem

  1. #1
    Registered User
    Join Date
    Jul 2004

    Exception problem


    I have the following code

    using namespace std;
    class MathError
    	int x,y;
    	MathError() : x(0), y(0)
    		cout << "Default Constructor " << this << endl;
    	MathError(int xl, int yl) : x(xl), y(yl)
    		cout << "Costructor1 " << this << endl;
    		cout << "Destructor " << this << endl;
    	//Seems to working fine with that
    	/*MathError(const MathError& me)
    		cout << "Copy constructor" << endl;
    		x = me.x;
    		y = me.y;
    	void message()
    		cout << "Exception : Divide by zero " << this << endl;
    int divide(int x, int y) throw(MathError);
    int main()
    	catch(MathError me)
    	return 0;
    int divide(int x, int y) throw(MathError)
    	if( y == 0)
    		throw MathError(x,y);
    	return (x/y);

    When I run it I have

    Costructor1 0012FEF0
    Destructor 0012FEF0
    Exception : Divide by zero 0012FF68
    Destructor 0012FF68
    Destructor 0012FEF8

    The last destructor what it is about?

    If you uncomment the copy constructor code, everything seems to working

    Costructor1 0012FF04
    Copy constructor
    Exception : Divide by zero 0012FF68
    Destructor 0012FF68
    Destructor 0012FF04

    Also if in catch (MathError me) I use a reference catch (MathError& me) again it seems to working

    The default constructor provided by compiler is the same as this I wrote and
    I donít want to pass a reference object, because I donít want to change the local exception copy in order to rethrown

    So can somebody explain what is happening?

  2. #2
    Registered User
    Join Date
    Jun 2005
    The reason is that stack unwinding (returning to the caller when an exception is active) involves [in principle at least] COPYING the exception to the caller. Which means the copy constructor is called. When you don't provide a copy constructor, then the compiler generates one for you and calls that (which means you get to see objects being destructed that you haven't seen being created).

    Catching by value also requires, in principle, a copy of the original constructor to be created.

    Where things get curly is that the standard allows the compiler to cheat, and avoid creating temporary copies of objects (the copies created during stack unwinding are temporaries) if the only way to detect the existance of the temporaries is by tracking calls to constructors or destructors. So an aggressive compiler is allowed to eliminate some of the copies, or destroy things in different orders. Which means the type of behaviour you're seeing will be compiler dependent (eg it depends on how aggressive your optimiser is)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  2. stack trace in exception
    By George2 in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2008, 03:00 AM
  3. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 12:12 PM
  4. problem with exception behavior
    By agarwaga in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2006, 04:32 PM
  5. Developing Custom Exception Handler :: C++
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 02-20-2002, 03:21 PM