try/catch scope question...

Discussion in 'C++' started by barcaroller, Sep 17, 2007.

  1. barcaroller

    barcaroller Guest

    I have an object that throws an exception when the constructor fails. I
    could construct it inside a try/catch block, but then the object is no
    longer visible outside the block.

    try
    {
    classA objA;
    }
    catch (...)
    {
    // handle the exception
    }

    objA.doStuff() // Wrong! Out of scope!



    I do not want to move the rest of the program inside the try/catch block.
    How do I work around this?
     
    barcaroller, Sep 17, 2007
    #1
    1. Advertising

  2. barcaroller

    Jerry Coffin Guest

    In article <fcmuu1$kj0$>, says...
    >
    > I have an object that throws an exception when the constructor fails. I
    > could construct it inside a try/catch block, but then the object is no
    > longer visible outside the block.
    >
    > try
    > {
    > classA objA;
    > }
    > catch (...)
    > {
    > // handle the exception
    > }
    >
    > objA.doStuff() // Wrong! Out of scope!
    >
    >
    >
    > I do not want to move the rest of the program inside the try/catch block.
    > How do I work around this?


    Putting the other code in the try block is generally preferred. Another
    possiblity is to create the object dynamically:

    classA *objA;

    try {
    objA = new ClassA;
    }
    catch (...) {
    // handle the exception -- and don't use '...' in the real code.
    }

    objA->doStuff(); // still in scope

    // and when you're done with it:

    delete objA;

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 18, 2007
    #2
    1. Advertising

  3. barcaroller wrote:
    > I have an object that throws an exception when the constructor fails. I
    > could construct it inside a try/catch block, but then the object is no
    > longer visible outside the block.
    >
    > try
    > {
    > classA objA;
    > }
    > catch (...)
    > {
    > // handle the exception
    > }
    >
    > objA.doStuff() // Wrong! Out of scope!
    >
    >
    >
    > I do not want to move the rest of the program inside the try/catch block.
    > How do I work around this?




    std::auto_ptr<classA> pobjA;

    try
    {
    pobjA = std::auto_ptr<classA>( new ClassA );
    }
    catch (...)
    {
    // handle
    }

    pobjA->DoStuff();


    If it must me on the stack, you can use an alloca (non-standard) see
    here: http://groups.google.com/group/comp.lang.c /msg/64bf343b7f5b76c1?.
     
    Gianni Mariani, Sep 18, 2007
    #3
  4. barcaroller

    red floyd Guest

    barcaroller wrote:
    > I have an object that throws an exception when the constructor fails. I
    > could construct it inside a try/catch block, but then the object is no
    > longer visible outside the block.
    >
    > try
    > {
    > classA objA;
    > }
    > catch (...)
    > {
    > // handle the exception
    > }
    >
    > objA.doStuff() // Wrong! Out of scope!
    >
    >
    >
    > I do not want to move the rest of the program inside the try/catch block.
    > How do I work around this?
    >


    You don't, that's the point. If the construction fails, then it's not a
    valid object, so you can't doStuff() with it. You do this:

    try
    {
    classA objA;

    objA.doStuff();
    }
    catch (...)
    {
    std::cout << "OMG!!! classA constructor failed!!!!" << std::endl;
    }

    Alternatively, and I don't recommend this method:

    std::auto_ptr<classA> objA( NULL );
    try
    {
    objA.reset(new classA);
    }
    catch(std::bad_alloc&)
    {
    // new failed
    }
    catch (...)
    {
    // classA constructor failed
    }
    if (objA.get())
    objA->doStuff();
     
    red floyd, Sep 18, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Kevin Spencer
    Aug 7, 2003
  2. bienwell
    Replies:
    4
    Views:
    3,854
    bienwell
    May 27, 2005
  3. Matt
    Replies:
    1
    Views:
    409
    Michael Rauscher
    Jun 12, 2004
  4. Matt
    Replies:
    5
    Views:
    422
    Adam Maass
    Jun 28, 2004
  5. Jackson A. Marshall

    try object out of scope before catch?

    Jackson A. Marshall, Jan 11, 2005, in forum: C++
    Replies:
    6
    Views:
    436
    Jackson A. Marshall
    Jan 11, 2005
Loading...

Share This Page