954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Failing to initialize - what should constructor do?

I'm starting writing class that should read configuration file and initialize some members with data from file. I decided to read this file in constructor. What should constructor do, if something goes wrong, and further construction of object have no sense? For now it displays message:

MultiReader::MultiReader(const char* m_sPath)
{
	fConfigFile.open(m_sPath);
	if(!fConfigFile)
		cout << "Cannot open specified configuration file: "
		<< m_sPath << endl;
}


Call destructor? break illegal here I guess.

MultiReader A;

What will be passed to A in case of such failure?

Cybulski
Light Poster
47 posts since Apr 2008
Reputation Points: 15
Solved Threads: 3
 

>What should constructor do, if something goes wrong, and further construction of object have no sense?
Log the error if necessary and throw an exception. Then the code that's trying to create the object can decide what to do.

Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 
>What should constructor do, if something goes wrong, and further construction of object have no sense? Log the error if necessary and throw an exception. Then the code that's trying to create the object can decide what to do.

Can you please give simpliest example?

Cybulski
Light Poster
47 posts since Apr 2008
Reputation Points: 15
Solved Threads: 3
 
#include <iostream>
#include <stdexcept>

class EdRules {
public:
  EdRules();
};

EdRules::EdRules()
{
  throw std::exception("Ed Rules!");
}

int main()
{
  try {
    EdRules ed;
  } catch (const std::exception& ex) {
    std::cout << ex.what() << '\n';
  }
}
Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

What if EdRules is part of another class?
And another class is part of another? Exception catching should be performed in each of these?

Cybulski
Light Poster
47 posts since Apr 2008
Reputation Points: 15
Solved Threads: 3
 

> Exception catching should be performed in each of these?
That's the beauty of exceptions. You only need to catch at the point you know how to handle the error. If another class and another don't know how to handle the exception, they just pass it on. If another class doesn't know how to handle the exception but another wants to log it, it can catch and then rethrow:

#include <iostream>
#include <exception>

class EdRules {
public:
  EdRules();
};

EdRules::EdRules()
{
  throw std::exception("Ed Rules!");
}

class AnotherClass {
  EdRules ed;
};

class Another {
  AnotherClass foo;
public:
  Another();
};

Another::Another() try: foo()
{}
catch (const std::exception& ex) {
  std::cout << "Logging in Another\n";
  throw;
}

int main()
{
  try {
    Another topLevel;
  } catch (const std::exception& ex) {
    std::cout << ex.what() << '\n';
  }
}
Radical Edward
Posting Pro
545 posts since May 2008
Reputation Points: 361
Solved Threads: 97
 

Pretty confusing I must say... I think I will skip it for now. I don't know final shape of my solution now.

Cybulski
Light Poster
47 posts since Apr 2008
Reputation Points: 15
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You