Jump to content
  • Advertisement
Sign in to follow this  
romer

(C++) Function try blocks and VS.NET 2003

This topic is 5064 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was doing a little sample program so I could get the hang of the syntax for a function try block in VS.NET 2003, and I noticed some behavior that I didn't expect. Here's the little program I was dealing with:
#include <iostream>
using std::cout;
using std::endl;


class Foo
{
    public:
        Foo( int i ) : num( i )
        {
            throw ( "exception" );  // purposely thrown for test purposes
        }

    private:
        int num;
};


class Bar
{
    public:
        Bar() try : foo( 5 )
        {
        }
        catch ( ... )
        {
            cout << "Bar::bar() handler" << endl;
        }

    private:
        Foo foo;
};


int main()
{
    Bar bar;
    return ( 0 );
}


When I build this small program and run it, the line "Bar::bar..." gets written to the console, but then I receive an unhandled exception error. I did this small mod to main:
int main()
{
    try
    {
        Bar bar;
    }
    catch ( ... )
    {
        cout << "main() handler" << endl;
    }
    return ( 0 );
}
And now both lines are output to the console, which leads me to believe the exception is being rethrown for some reason. I thought using the function try block at Bar's constructor would stop the exception from being propagated any further. Do I have a wrong impression, or is this just some quirky non-standard behavior?

Share this post


Link to post
Share on other sites
Advertisement
It's standard behaviour and it's specific to constructor try-catch blocks. If a base or subobject of an object throws an exception during construction of the object then an exception must be thrown. The object never actually exists, so the constructor cannot run to completion. So for constructor try-catch blocks the C++ specification states that if the end of the catch block is reached without an exception having been thrown the originally caught exception is rethrown. See GOTW - Constructor Failures for more details.

Enigma

Share this post


Link to post
Share on other sites
Ah makes sense. In fact, that's what I was shooting for (translating the caught exception into a different type of exception), so it all ended up working in the end. Thanks for the help, and thanks also for the article reference! Very insightful. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!