============================MODULE21======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ========================================================================= //C structured exception handling //and C++ exception handling. For VC++/VC++ .Net change the //header files to the following accordingly...same for other examples //#include //using namespace std; #include //function prototype... void TestCFunct(void); int main() { //C++ try block... try { //function calls... TestCFunct(); } //catch block... catch(...) { cout<<"Caught the exception, C style..."<< endl; } return 0; } //function definition... void TestCFunct() { //structured handling exception... __try { int p, r = 2, q = 0; //exception should be raised here //divide by 0... p = r*(10/q); } __finally { cout<<"In __finally" << endl; //finding the appropriate catch... } } ---------------------------------------------------------------------------------------- //simple try, throw...catch #include int main() { //declare char pointer char* buff; //try block... try { //allocate storage for char object... buff = new char[1024]; //do a test, if allocation fails... if(buff == 0) throw "Memory allocation failure!"; //if allocation successful, display //the following message, bypass //the catch block... else cout< #include int main () { try { char * teststr; teststr = new char [10]; //test, if memory allocation fails then, //throws this error to the matching catch... if (teststr == NULL) throw "Allocation failure"; for (int i=0; i<=15; i++) { //another test, if n>9, throw this error //to the respective catch.. if (i>9) throw i; teststr[i]='z'; cout<<"teststr["< 9, by displaying some //error message... catch (int j) { cout<<"The exception: "; cout<<"index "< void Funct(); int main() { try { Funct(); } catch(double) { cerr<<"caught a double type..."< void TestFunct(void); //class Test1 declaration... class Test1 { public: Test1(){}; ~Test1(){}; const char *TestShow() const { cout<<"In class member function *TestShow():\n"; return " Exception in Test1 class."; } }; //another class declaration, DestrTest... class DestrTest { public: DestrTest(); ~DestrTest(); }; //constructor class implementation DestrTest::DestrTest() { cout<<"Next, in constructor DestrTest():\n"; cout<<" Constructing the DestrTest...\n"; } //destructor class implementation DestrTest::~DestrTest() { cout<<"Next, in destructor ~DestrTest():\n"; cout<<" Destructing the DestrTest...\n"; } void TestFunct() { //instantiate an object, constructor invoked... DestrTest p; cout<<"Next in TestFunct(): \n Throwing Test1 type exception...\n"; //first throw... throw Test1(); } int main() { cout<<"Starting in main()...\n"; try { cout<<"Now, in the try block: \n Calling TestFunct()...\n"; TestFunct(); } //instantiate another object, constructor invoked... catch(Test1 q) { cout<<"Next, in catch handler:\n"; cout<<" Caught Test1 type exception...\n"; cout< //this empty throw will be ignored... void Nothing() throw() { cout<<"In Nothing(), empty throw..."< //base class class Test1 {}; //derived class class Test2 : public Test1 {}; void Funct(); int main() { try { //function call, go to Funct() Funct(); } catch(const Test1&) { cerr<<"Caught a Test1 type, base class..."< //exit() #include //set_terminate() #include void Funct() { cout<<"Funct() was called by terminate()."< //handler function void handler() {cout<<"In the handler()\n";} //int throw... void Funct1(void) throw(int) { static int x = 1; cout<<"Funct1() call #"< #include #include class Test1 { virtual Funct() {}; }; int main () { try { Test1 * var = NULL; typeid (*var); } catch (std::exception& typevar) { cout<<"Exception: "< #include using namespace std; int main( ) { try { string strg1("Test"); string strg2("ing"); strg1.append(strg2, 4, 2); cout< #include using namespace std; class Myshape { public: virtual void myvirtualfunc() const {} }; class mytriangle: public Myshape { public: virtual void myvirtualfunc() const { }; }; int main() { Myshape Myshape_instance; Myshape &ref_Myshape = Myshape_instance; try { //try the run time typecasting, dynamic_cast mytriangle &ref_mytriangle = dynamic_cast(ref_Myshape); } catch (bad_cast) { cout<<"Can't do the dynamic_cast lor!!!"< #include using namespace std; int main() { char* ptr; unsigned long int Test = sizeof(size_t(0)/3); cout<<"The size of variable Test = "< #include using namespace std; void myfunction() { cout<<"Testing myfunction()."< #include using namespace std; class Test { public: //object for a class needs vtable //for the rtti... Test(); virtual ~Test(); }; int main() { Test *ptrvar = NULL; try { //the error condition cout< using namespace std; int main() { try { throw domain_error("Some error with your domain!"); } catch (exception &err) { cerr<<"Caught: "< #include using namespace std; int main() { try { //binary wrongly represented by char X //template based… bitset<32> bitset(string("0101001X01010110000")); } catch (exception &err) { cerr<<"Caught "< using namespace std; int main() { //runtime_error try { locale testlocale("Something"); } catch(exception &err) { cerr<<"Caught "< #include using namespace std; int main() { try { //template based… bitset<100> bitset; bitset[99] = 1; bitset[0] = 1; //to_ulong(), converts a bitset object to the integer //that would generate the sequence of bits unsigned long Test = bitset.to_ulong(); } catch(exception &err) { cerr<<"Caught "< using namespace std; int main() { try { throw range_error("Some error in the range!"); } catch(exception &Test) { cerr<<"Caught: "< using namespace std; int main() { try { throw underflow_error("The negative storage?"); } catch(exception &Test) { cerr<<"Caught: "< using namespace std; void TestFunct(void); //class Test1 declaration... class Test1 { public: Test1(){}; ~Test1(){}; const char *TestShow() const { cout<<"In class member function *TestShow():\n"; return " Exception in Test1 class."; } }; //another class declaration, DestrTest... class DestrTest { public: DestrTest(); ~DestrTest(); }; //constructor class implementation DestrTest::DestrTest() { cout<<"Next, in constructor DestrTest():\n"; cout<<" Constructing the DestrTest...\n"; } //destructor class implementation DestrTest::~DestrTest() { cout<<"Next, in destructor ~DestrTest():\n"; cout<<" Destructing the DestrTest...\n"; } void TestFunct() { //instantiate an object, constructor invoked... DestrTest p; cout<<"Next in TestFunct(): \n Throwing Test1 type exception...\n"; //first throw... throw Test1(); } int main() { cout<<"Starting in main()...\n"; try { cout<<"Now, in the try block: \n Calling TestFunct()...\n"; TestFunct(); } //instantiate another object, constructor invoked... catch(Test1 q) { cout<<"Next, in catch handler:\n"; cout<<" Caught Test1 type exception...\n"; cout<