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;
    }
}
xiawi
  • 1,772
  • 4
  • 19
  • 21

1 Answers1

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++.

YSC
  • 38,212
  • 9
  • 96
  • 149