Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What to do when construction fails?

Posted on 1998-06-29
2
Medium Priority
?
172 Views
Last Modified: 2010-04-01
Lets say I have a constructor that does some operations, for some reason one or more of these operations fail.  What is the best way to set some sort of error condition?  

A a = new A("filename");

say the filename doesn't exist, now probably any operation I do with a will also fail.  Do I use exceptions?  Add a class function to check for valid creation?


I am not dealing with IO in any sort in my situation, just using it to clearify my problem
0
Comment
Question by:micah
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 400 total points
ID: 1166887
Yes, you have two choices:

1. throw an exception and expect the client of the class to catch any possible exceptions.

2. Put the state of the object into an error state so that any methods called will be an error. Also you may need to provide a GetState() accessor to determine if the object is in an error state or not. This requires the client to consciously to call the GetState() method to determine if the class is valid or not.

I would opt for the first one if you have access to all of the client's code simply because it is typically unlikely for the exception to occur and hence the amount of code necessary to change will be quite local.

In the second case, it requires more changes because each time a method is called, the state of the object needs to be checked.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1166888
There is actually a third choice, but I would probably opt for one of the others.  But the third choice is you can return an error code of some sort using a reference parameter.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question