try/catch scope question...

B

barcaroller

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?
 
J

Jerry Coffin

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;
 
G

Gianni Mariani

barcaroller said:
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?.
 
R

red floyd

barcaroller said:
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();
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top