Solved

What to do when construction fails?

Posted on 1998-06-29
2
169 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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