try catch debug erro

i have this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class A
{
private:
	int a;
public:
	A(int i) {a=i;};
	~A() {if (a<0) throw a;}
	int geta(void) {if (a<0) throw a;return a;};
};

void main( void )
{

try
{
	A  a(-25);
	cout<<a.geta();
}
catch(int i)
{
	cout<<"caught you";
	
}

int k;
cin>>k;
}


and a debug error exists
i dont unerstand why..
I assume that by 'debug error' you mean that your exception handler writes out 'caught you'.
The reason that it does this is pretty obvious. First you construct an instance of A with A.a = -25, then you call geta(), and geta() throws if A.a is negative. Am I missing your point?
Last edited on
yes it doesnt write anything an an error window opens..
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;


class A
{
private:
  int a;
public:
  A(int i) {a=i;};
  ~A() {if (a<0) throw a;}
  int geta(void) {if (a<0) throw a;return a;};
};


int main()
{
  try
  {
    A  a(-25);
    cout<<a.geta();
  }
  catch(int i)
  {
    cout<<"caught you";
  }

  int k;
  cin>>k;

  return 0;
}
Indeed, as kspangsege said, the geta code throws exception as you should expect. There is also another issue. When geta throws, the stack is unwound and the destructor of A is called. The destructor raises another exception before the previous one is caught. When an exception is raised while another one is still active, terminate() will be called. This is practically a program crash.

Destructors should not throw, and they should also subsume all exceptions that are thrown within their body. If you need sophisticated error management, then you need a method that you can call before the object is destroyed.

There have been attempts to make the error reporting behavior of the destructor adaptable, like providing the uncaught_exception() function to check for the presence of currently active exceptions. The solutions are however conceptually underdeveloped and do not cater for all needs. Also, throwing in destructor while performing array delete produces undefined behavior anyways.

Regards
what is the difference betwwen that and previous code??
I think the real error lies here:
 
  ~A() {if (a<0) throw a;}

This is what happens:
* After the call of geta an exception will be raised (as wanted).
* Now stack unwinding happens.
* During stack unwinding all variables in the current scope (i.e. a) will be destroyed
* Now the destructor throws another exception
* An exception during stack unwinding causes terminate to be called which exits the program (or opens a debug window)

So, never allow exceptions to leave your destructor!

See also http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.13
(there you'll also find some topics on manually destructing objects of your other post)

edit: too slow ;-)
Last edited on
then from the previous posts..

"When an exception is raised while another one is still active, terminate() will be called."

this is the problem or

"During stack unwinding all variables in the current scope (i.e. a) will be destroyed"


??
The first point

"When an exception is raised while another one is still active, terminate() will be called."

is probably the reason you get a debug window, because you editor/IDE sees this as a program crash.
vagelis wrote:
[...] this is the problem or [...] ??

Both.

During stack unwinding all variables in the current scope (i.e. a) will be destroyed.

This means that when geta throws, the destructor of a will be called.
But the destructor of a also throws, therefore...

When an exception is raised while another one is still active, terminate() will be called.

So, as the guys above said, don't throw inside a destructor.
Last edited on
Yes, probably terminate() causes the debug window.
ok thanks..!!
Topic archived. No new replies allowed.