Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:06:50+00:00 2026-06-15T05:06:50+00:00

I have created a class and in the constructor I do something like: MyClass(string

  • 0

I have created a class and in the constructor I do something like:

MyClass(string file)
{
    ifstream str;
    str.open (file, ifstream::in);

    // initialize class variables based on values from file

    str.close();
}

How would I know though if there was an error while reading the file and the values were not initialized correctly? Is the above the wrong way to do it? How else could I proceed if I want my variables to be initialized from a file in the constructor?

Edit, to clarify: What I am asking is in a statement like:

MyClass myclass("path/to/my/file.txt");

how could I know that everything was initialized correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:06:54+00:00Added an answer on June 15, 2026 at 5:06 am

    One possibility is to use exceptions:

    ...
    ifstream str;
    str.exceptions(std::ifstream::failbit);
    // continue as before
    

    Then, if your constructor throws an exception, you know something didn’t work.

    Another possibility is to detect the failure in the constructor and set a state variable which can be examined by the calling code.

    Here are both solutions:

    #include <iostream>
    #include <exception>
    #include <string>
    #include <fstream>
    
    struct MyClass1 {
      int i;
      MyClass1(const char * iniFile) {
        std::ifstream in;
        in.exceptions(std::ifstream::failbit);
        in.open(iniFile);
        in >> i;
      }
    };
    
    struct MyClass2 {
      int i;
      bool isValid;
      MyClass2(const char * iniFile) {
        try {
          std::ifstream in;
          in.exceptions(std::ifstream::failbit);
          in.open(iniFile);
          in >> i;
          isValid = true;
        } catch(std::ios_base::failure &fail) {
          isValid = false;
        }
      }
    };
    
    int main () {
      try {
        MyClass1 mc1("somefile.txt");
      } catch(std::exception& fail) {
        std::cout << "oops 1\n";
      }
    
      MyClass2 mc2("somefile.txt");
      if(!mc2.isValid) {
        std::cout << "oops 2\n";
      }
    }
    

    Assuming that somefile.txt does not exist, that should print two lines of “oops”.

    Finally, a third possibility is to ensure that the constructor can never fail. In your case, you could provide default values in the catch block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class derived from NSImageView. However, the classes's constructor (init) is
I have created a class for fetching server-side data in my iOS-App, based on
Is it possible to have a class that behaves like a string but allows
I have something like: #include MyImage.hpp // MyImage wraps the Qt library image class
I have a generic class like below: public class MyClass<T, TProperty> { MyClass(Expression<Func<T, TProperty>>
I have a class file called MarkerCustom. MarkerCustom has a constructor that takes three
I have created a class named Times and I have to construct 4 overloaded
I am following the guide at http://guides.rubyonrails.org/association_basics.html and I have created class Customer <
I have created a class which extends View class. public class SplashScreen extends View
I have created a class called Class1, and in another module, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.