?
Solved

What to do when construction fails?

Posted on 1998-06-29
2
Medium Priority
?
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

718 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