Solved

What to do when construction fails?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 82
gdb doesn't stop on breakpoint 2 89
Outlook 13 38
One named event, multiple event handlers 2 24
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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

790 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