www.digitalmars.com         C & C++   DMDScript  

c++ - throwing an exception in a new expression

reply Christof Meerwald <cmeerw web.de> writes:
Here is a test-case (I get an "abnormal program termination" message):

#include <stdio.h>

struct A
{
};

struct B
{
  B()
  {
    throw A();
  }
};

int main()
{
  try
  {
    B *b = new B();
    //B b; // this one works
  }
  catch (const A &a)
  {
    printf("caught A\n");
  }

  return 0;
}


BTW, the problem also occurs when the exception isn't thrown directly in the
constructor, but somewhere in the new expression:

#include <stdio.h>

struct A
{
};

struct B
{
  B(int i)
  { }
};

int f()
{
  throw A();
  return 0;
}

int main()
{
  try
  {
    B *b = new B(f());
    //B b(f());
  }
  catch (const A &a)
  {
    printf("caught A\n");
  }

  return 0;
}


bye, Christof

-- 
http://cmeerw.org                                 JID: cmeerw jabber.at
mailto cmeerw at web.de

...and what have you contributed to the Net?
Nov 09 2002
parent reply "Walter" <walter digitalmars.com> writes:
Got it, thanks. -Walter
Nov 10 2002
next sibling parent reply Richard <fractal clark.net> writes:
In article <aql8j0$hol$1 digitaldaemon.com>, Walter says...
Got it, thanks. -Walter
mmm.. I get something similiar in instantiation a template object. I got a template c'tor that like: mytemp() throw (Exception) And if I put the instantiation of the object inside try block and then later throw any exception, I can't catch the exception and get A.B. Normal. I was not able to isolate the problem, and am not using new operator. If I move the instantiation out of the try block, I get a catch on the Exception. Richard
Nov 13 2002
parent Richard <fractal clark.net> writes:
Well, 

Ok, it turned out to be user error again. This time an access violation reached
by the d'tor of the template. I was assigning a null object to a reference.
Similiar to:

void f(const Object& check) {
Object* ptr = 0;
Object& ref = *ptr;
if (ref == check)
// do something..
}

So stack unwind caught it, and generated an abnormal program termination.

Richard
Nov 13 2002
prev sibling parent reply Richard <fractal clark.net> writes:
In article <aql8j0$hol$1 digitaldaemon.com>, Walter says...
Got it, thanks. -Walter
After I fixed my access violation, I found I was using new operator in a critical template and throwing exceptions during the construction chain. There's no useful way to create these objects on the stack or as static. Sorry to sound pushy, but can you give some indication about when a fix for abend when throwing an exeption in a new expression might be available. Feel free to say its going to take a while, and I'll hack the exisiting startup code to handle it. Since its somewhat onerous, and the result temporary, I figured I'd ask before doing it. Richard
Nov 14 2002
parent "Walter" <walter digitalmars.com> writes:
It'll take a while. I'm pretty buried at the moment. -Walter

"Richard" <fractal clark.net> wrote in message
news:ar092i$1as0$1 digitaldaemon.com...
 In article <aql8j0$hol$1 digitaldaemon.com>, Walter says...
Got it, thanks. -Walter
After I fixed my access violation, I found I was using new operator in a critical template and throwing exceptions during the construction chain.
There's
 no useful way to create these objects on the stack or as static.

 Sorry to sound pushy, but can you give some indication about when a fix
for
 abend when throwing an exeption in a new expression might be available.

 Feel free to say its going to take a while, and I'll hack the exisiting
startup
 code to handle it. Since its somewhat onerous, and the result temporary, I
 figured I'd ask before doing it.

 Richard
Nov 14 2002