Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > try/catch scope question...

Reply
Thread Tools

try/catch scope question...

 
 
barcaroller
Guest
Posts: n/a
 
      09-17-2007

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?


 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      09-17-2007
In article <fcmuu1$kj0$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-17-2007
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....f343b7f5b76c1?.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      09-17-2007
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();

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
CSPEC issue: lossing scope (or incorrect scope) in cspec subroutine. balldarrens@gmail.com Perl Misc 0 02-05-2009 08:42 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments