Solved

What to do when construction fails?

Posted on 1998-06-29
2
164 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 100 total points
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now