473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4454
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ChrisB | last post by:
Hello All: I notice that when using try/catch blocks in C#, variables declared in the try block go out of scope in the finally block. So, for example, the following code generates a compiler...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
2
by: robert | last post by:
When employing complex UI libs (wx, win32ui, ..) and other extension libs, nice "only Python stack traces" remain a myth. Currently I'm hunting again a rare C-level crash bug of a Python based...
7
by: Rob R. Ainscough | last post by:
In VB6 I can use Resume Next to execute the line of coding following the line that cause an exception. There doesn't appear to be anything similiar when using Try...Catch -- so how can one resume...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
3
by: John Rivers | last post by:
try catch blocks create a new scope i don't think that they should for example, to add error handling to this statement: int x = 1; you have to rewrite it as a seperate declaration and...
12
by: Angus | last post by:
Hello I have a class I am using which raises an exception in its constructor if certain things aren't in place. I can easily create the situation where an exception is raised. If I use the...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.