469,921 Members | 2,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,921 developers. It's quick & easy.

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?
Sep 17 '07 #1
3 4223
In article <fc**********@aioe.org>, ba*********@music.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.
Sep 17 '07 #2
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<classApobjA;

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....f343b7f5b76c1?.
Sep 18 '07 #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?
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<classAobjA( NULL );
try
{
objA.reset(new classA);
}
catch(std::bad_alloc&)
{
// new failed
}
catch (...)
{
// classA constructor failed
}
if (objA.get())
objA->doStuff();

Sep 18 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by VB Programmer | last post: by
7 posts views Thread by Rob R. Ainscough | last post: by
23 posts views Thread by pigeonrandle | last post: by
3 posts views Thread by John Rivers | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.