Click to See Complete Forum and Search --> : exception and global variable


George2
January 28th, 2008, 03:33 AM
Hello everyone,


Bjarne mentioned in his book, that there is no way to catch exceptions from the initialization of global variables (section 14.7). But I do not think it is true, because we can add function try block to its constructor, and catch appropriate types of exceptions or using catch(...) to catch all exceptions.

Am I correct? Any comments?


thanks in advance,
George

laserlight
January 28th, 2008, 03:50 AM
Why not post some code that would serve as a counterexample to Stroustrup's statement?

George2
January 28th, 2008, 04:05 AM
Hi laserlight,


Here is the code to verify exception from constructor of global variable could be caught. Anything wrong?


#include <iostream>

using namespace std;

class Foo{

public:

Foo ();
};

Foo::Foo()
try{
cout << "constructing" << endl;

throw(0);

} catch (...)
{
cout << "catch exception in constructor" << endl;
}


Foo foo;

int main()
{
int i = 0;

cout << i;

return 0;
}


Why not post some code that would serve as a counterexample to Stroustrup's statement?


regards,
George