Why would this excpetion handling is not working

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 37
Reputation: gcardonav is an unknown quantity at this point 
Solved Threads: 0
gcardonav gcardonav is offline Offline
Light Poster

Why would this excpetion handling is not working

 
0
  #1
Jan 30th, 2008
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

  1. throw de;

Any idea why. Thank you once again for the help

GCard
This is the error.h file
  1. #include <fstream>
  2. #include <memory>
  3. #include <iostream>
  4. using namespace std;
  5. class error
  6. {
  7. protected:
  8. fstream errfile;
  9. public:
  10. void printmsg(char *msg);
  11. void logmsg(char *msg);
  12. };
  13. void error::printmsg(char *msg)
  14. {
  15. cout << msg;
  16. }
  17. void error::logmsg(char *msg)
  18. {
  19. errfile.open("error.log",ios::app);
  20. errfile << msg;
  21. }
  22. class fileerror:public error
  23. {
  24. };
  25. class divisionbyzero:public error
  26. {
  27. }

And this is the program
  1. #include "error.h"
  2. #include <iostream>
  3. using namespace std;
  4. fileerror fe;
  5. divisionbyzero de;
  6. int main()
  7. {
  8. fstream f;
  9. float dividend, divisor,quotient;
  10. try
  11. {
  12. cout << "Enter a number " << endl;
  13. cin >> dividend;
  14. cout << "Enter another number " << endl;
  15. cin >> divisor;
  16. // if the divisor is zero we should
  17. // get the divisionbyzero
  18. if(divisor==0)
  19. throw de;
  20. quotient = dividend/divisor;
  21. cout << quotient;
  22. // if there is no file then we should get
  23. // the fileerror
  24. f.open("somefile.txt",ios::in);
  25. if(!f)
  26. throw fe;
  27. }
  28. catch(divisionbyzero d)
  29. {
  30. d.printmsg("Division by zero \n");
  31. }
  32. catch(fileerror f)
  33. {
  34. f.printmsg("File not found error \n");
  35. }
  36. catch(...)
  37. {
  38. cout<< "Unknown error! \n";
  39. }
  40. return 0;
  41. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,653
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why would this excpetion handling is not working

 
0
  #2
Jan 30th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Why would this excpetion handling is not working

 
0
  #3
Jan 30th, 2008
in
throw expression ;
a. the (static) type of expression 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 558 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC