Greeting guys
Rookie here again. By the way, thanks for all the help. Anyway to business. I am trying to compile a program to understand how to handle exceptions using inheritance. I have created first error.h where all the classes and child classes are defined. Then when I create my main code and try to compile it it gives me an error on the following part
throw de;
Any idea why. Thank you once again for the help
GCard
This is the error.h file
#include <fstream>
#include <memory>
#include <iostream>
using namespace std;
class error
{
protected:
fstream errfile;
public:
void printmsg(char *msg);
void logmsg(char *msg);
};
void error::printmsg(char *msg)
{
cout << msg;
}
void error::logmsg(char *msg)
{
errfile.open("error.log",ios::app);
errfile << msg;
}
class fileerror:public error
{
};
class divisionbyzero:public error
{
}
And this is the program
#include "error.h"
#include <iostream>
using namespace std;
fileerror fe;
divisionbyzero de;
int main()
{
fstream f;
float dividend, divisor,quotient;
try
{
cout << "Enter a number " << endl;
cin >> dividend;
cout << "Enter another number " << endl;
cin >> divisor;
// if the divisor is zero we should
// get the divisionbyzero
if(divisor==0)
throw de;
quotient = dividend/divisor;
cout << quotient;
// if there is no file then we should get
// the fileerror
f.open("somefile.txt",ios::in);
if(!f)
throw fe;
}
catch(divisionbyzero d)
{
d.printmsg("Division by zero \n");
}
catch(fileerror f)
{
f.printmsg("File not found error \n");
}
catch(...)
{
cout<< "Unknown error! \n";
}
return 0;
}