0

I'm a student in the process of learning C, I am trying to make a user defined exception class. I watched a few videos and read a few tutorials and ended up with this program. However every time I try to run the program and throw the exception the program closes and I get the fallowing message with the option to change some settings.

Unhandled exception at 0x75BF1812 in EC1.exe: Microsoft C++ exception: FileNotFound at memory location 0x0073F4BC. occurred

I tried to look up this message but I didn't find anything. Any advice on how to move forward or what I am doing wrong would be greatly appreciated.

#include <iostream>
#include <fstream> 
#include <iomanip>
#include <string>

class FileNotFound : public std::exception
{
public:
    const char* what()
    {
        return ("Exception: File not found.\n");
    }
};


const int NO_OF_COMPUTES = 2;

struct computerType
{
    std::string cID;
    std::string manufacturer;
    std::string name;
    std::string cpuSpeed;
    int ramSize;
    double price;
};


void getComputer(std::ifstream& infile);

/*void writeComputer(ofstream& outfile, computerType list[],
    int listSize);*/


int main()
{

    std::ifstream infile;    //input file stream variable
    std::ofstream outfile;   //output file stream variable

    std::string inputFile;   //variable to hold the input file name
    std::string outputFile;  //variable to hold the output file name





    computerType computerTypeList[NO_OF_COMPUTES];

    std::cout << "Enter the input file name: "; 
    std::cin >> inputFile;                               
    std::cout << std::endl;

    infile.open(inputFile.c_str());                 

    if (!infile)
    {
        FileNotFound a;
        throw a;
    }

    getComputer(infile);


    infile.close();                                 
    outfile.close();                              
    system("pause");
    return 0;
}

void getComputer(std::ifstream& infile)
{
    int index;
    std::string cID;
    std::string manufacturer;
    std::string name;
    std::string cpuSpeed;
    int ramSize;
    double price;

    infile >> cID;
    while (infile)
    {
        infile >> manufacturer >> name >> cpuSpeed >> price;
        std::cout << cID << " " << manufacturer << " " << name << " " << cpuSpeed << " " << price;
        infile >> cID;
    }
}
7
  • 5
    Where are you catching the exception? What result did you expect by throwing the exception? Commented Feb 11, 2019 at 13:43
  • 2
    what is only called if you call it in a catch block. Not sure what you were expecting. Commented Feb 11, 2019 at 13:44
  • The error message is merely saying that a "FileNotFound" exception was thrown and not caught, and doesn't have a message. Commented Feb 11, 2019 at 13:46
  • 1
    Get a good book instead of watching videos and tutorials - they're not worth the money you're paying for them. Commented Feb 11, 2019 at 13:47
  • 1
    You can cut down your example to int main() { throw FileNotFound();}. Perhaps that makes it clearer what's (not) happening. Commented Feb 11, 2019 at 13:49

1 Answer 1

3

std::exception::what has the signature:

virtual const char* what() const noexcept;

Yours misses the const qualifier: you're not overriding it. It should be:

struct FileNotFound : std::exception
{
    const char* what() const noexcept override
    {
        return "Exception: File not found.\n";
    }
};

But it won't solve your problem: you're not catching the exception. If a non handled exception is thrown in main (among other situations like stack unwinding), abort() is called and your system may print an helper message like yours does. You need to document yourself about exceptions in C++.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.