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

Why would this excpetion handling is not working

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;
   }
gcardonav
Junior Poster in Training
54 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

After a little testing it appears that the c++ doesn't support fstream in that error class when used in a throw statement. Comment out the fstream object and it the program compiles ok. I'm not enough of an expert in this to tell you why that is happening.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

in

throw <strong>expression</strong> ;



a. the (static) type ofexpression cannot be an incomplete type.
b. the static type of the expression is used to initialize a temporary object of that type (through the type's copy-constructor) into a special memory location which must stay valid during the subsequent stack unwinding. (the memory location used is completely implementation-dependent.) this temporary object is destroyed (its destructor is invoked and then its memory freed) when the stack unwinding completes.
c. the C++ specification suggests that the expression can be viewed in the same way as an argument to a function call (passed by value). therefore, if the expression is an object, the copy constructor and destructor of that object must be accessible.

c++ stream objects are not copyable; so the class error (which contains a stream object) is also not copyable. and the derived classes fileerror and divisionbyzero are also not copyable.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You