| | |
Why would this excpetion handling is not working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 37
Reputation:
Solved Threads: 0
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
Any idea why. Thank you once again for the help
GCard
This is the error.h file
And this is the program
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
C++ Syntax (Toggle Plain Text)
throw de;
Any idea why. Thank you once again for the help
GCard
This is the error.h file
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
#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; }
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
in 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.
throw expression ;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.
![]() |
Similar Threads
- Java Client/Server (Java)
Other Threads in the C++ Forum
- Previous Thread: Lost toolbar after minimize
- Next Thread: IPO Chart Assistance Please
Views: 558 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






