CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: exception and global variable

    Why not post some code that would serve as a counterexample to Stroustrup's statement?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    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;
    }
    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured