|
-
January 28th, 2008, 04:33 AM
#1
exception and global variable
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
-
January 28th, 2008, 04:50 AM
#2
Re: exception and global variable
Why not post some code that would serve as a counterexample to Stroustrup's statement?
-
January 28th, 2008, 05:05 AM
#3
Re: exception and global variable
Hi laserlight,
Here is the code to verify exception from constructor of global variable could be caught. Anything wrong?
Code:
#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;
}
 Originally Posted by laserlight
Why not post some code that would serve as a counterexample to Stroustrup's statement?
regards,
George
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|