This is the mail archive of the gcc@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]

Exception problems


Why doesn't this work?  I think it should.

$ ./try2
C number 3
C number 2
throwing 3
C number 1
throwing 2
C number 0
throwing 1
caught 1
Aborted (core dumped)
$ cat try2.cc
#include <stdio.h>

class C
{
public:
  C ();
  ~C ();
private:
  int id;
  static int count;
};

int C::count = 3;

int
main ()
{
  C c;
  return 0;
}

C::C () : id (count--)
{
  printf ("C number %d\n", id);
}

C::~C ()
{
  try
    {
      if (id > 0)
	{
	  C c;
	  printf ("throwing %d\n", id);
	  throw id;
	}
    }
  catch (int x)
    {
      printf ("caught %d\n", x);
    }
}
$ 

The problem is that when the exception is rethrown after the cleanup for
the try block is executed the values of __eh_type and __eh_value has been
clobbered by the exception inside C::~C.  There should be a way to
preserve these variables around a cleanup block, between the points that
are marked with (1) and (2) below:

destruct c(2)
C::~C:
  construct c(1)
  set __eh_value, __eh_type, __eh_cleanup, __eh_pc
  throw exception
  execute cleanup block				<---(1)
    destruct c(1)
    C::~C:
      construct c(0)
      set __eh_value, __eh_type, __eh_cleanup, __eh_pc
      throw exception
      destruct c(0)
      set __eh_pc
      rethrow exception
      set __eh_in_catch
      execute catch block
      call __eh_cleanup (deletes __eh_value)
      clear __eh_type, __eh_in_catch
  end of cleanup block				<---(2)
  set __eh_pc
  rethrow exception
    __eh_type is NULL -> terminate


-- 
Andreas Schwab                                      "And now for something
schwab@issan.informatik.uni-dortmund.de              completely different"


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