try/catch scope question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barcaroller

    #1

    try/catch scope question...


    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?


  • Jerry Coffin

    #2
    Re: try/catch scope question...

    In article <fcmuu1$kj0$1@a ioe.org>, barcaroller@mus ic.net 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.

    Comment

    • Gianni Mariani

      #3
      Re: try/catch scope question...

      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<c lassApobjA;

      try
      {
      pobjA = std::auto_ptr<c lassA>( 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....f343b7f5b76c1?.

      Comment

      • red floyd

        #4
        Re: try/catch scope question...

        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<c lassAobjA( NULL );
        try
        {
        objA.reset(new classA);
        }
        catch(std::bad_ alloc&)
        {
        // new failed
        }
        catch (...)
        {
        // classA constructor failed
        }
        if (objA.get())
        objA->doStuff();

        Comment

        Working...