PROGRAM | DEMONSTRATES THE USE OF |
---|---|
except1.cpp | Demonstrates exception handling with the try and catch functions and the operator throw. |
except1a.cpp | Demonstrates exception handling with a throw statement that has no catch to receive the value. |
except1b.cpp | Demonstrates the throw of a variable instead of a value. |
except1c.cpp | Demonstrates the throw of a variable instead of a value. It is the same as except1b.cpp except the signature of the catch() is only a data type and not a data type followed by a variable. However, since there is not variable to pass in the value passed can not be used in the body of the catch(). |
except2.cpp | Demonstrates the throw of a programmer defined data type rather than a system data type as in except1c.cpp. |
except3.cpp | Demonstrates the throw of a value for which there is no catch. In this case the program is aborted |
except4.cpp | Demonstrates the throw of a variable. It has several calls to the function but not all execute the throw. |
except5.cpp | Demonstrates the throw of a variable which could be any data type or a string. |
except6.cpp | Demonstrates the use of a default catch() which catches all throws. |
except7.cpp | Demonstrates the default catch() but as only one of several alternatives. This is a better approach than except6.cpp where the default caught everything. |
except8.cpp | Demonstrates the restriction on a function as to the type of throws which can occur. |
except9.cpp | Demonstrates that a catch() may contain a throw as well as a try block and that a throw does not need to have an argument. |
fractions.cpp | Demonstrates how a class can be made robust by using exceptions. |
ratio.cpp | Contains an example of throwing a base and a derived object to determine which catch() should come first. |
ratio2.cpp | Contains an example of throwing a base and a derived object to determine which catch() should come first. The catches in this example are reversed those in ratio.cpp |
// program_id except1.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates // exception handling with // the try and catch functions // and the operator throw. // #include <iostream> using namespace std; void main() { cout << "This is the start." << endl; try { cout << "Got into the try block." << endl; throw 34; cout << "This line of code is not reached." << endl; } catch(int i) { cout << "The program throw number " << i << endl; } cout << "The program will now end" << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except1a.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates // exception handling with // a throw statement that has no catch // to receive the value. The program will abort. // #include <iostream> using namespace std; void main() { cout << "This is the start." << endl; try { cout << "Got into the try block." << endl; throw 34; cout << "This line of code is not reached." << endl; } catch(char i) { cout << "The program throw number " << i << endl; } cout << "The program will now end" << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except1b.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a variable instead // of a value. Compare this program // with except1c.cpp // #include <iostream> using namespace std; // the variable theValue is passed to catch() by value // since the variable theValue has moved out of scope. void main() { cout << "This is the start." << endl; try { int theValue = 34; cout << "Got into the try block." << endl; throw theValue; cout << "This line of code is not reached." << endl; } catch(int i) { cout << "The program throw number " << i << endl; } cout << "The program will now end" << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except1c.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a variable instead // of a value. It is the same as // except1b.cpp except the signature // of the catch() is only a data // type and not a data type followed // by a variable. However, since // there is not a variable to pass in // the value passed can not be used // in the body of the catch(). // #include <iostream> using namespace std; void main() { cout << "This is the start." << endl; try { int value = 34; cout << "Got into the try block." << endl; throw value; cout << "This line of code is not reached." << endl; } catch(int) { cout << "The program throw an integer" << endl; } cout << "The program will now end" << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except2.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a programmer defined // data type rather than a system // data type as in except1c.cpp. // #include<iostream> using namespace std; class DivideByZeroError { private: const char *message; public: // The colin in the definition of DivieByZeroError() // is required since message is a constant. // DivideByZeroError() : message("Divide by zero"){ } // Notice that printMessage() is a const function // and therefore it may manipulate the const data // member message. // void printMessage() const {cout << message;} }; float quotient(int num1, int num2) { // Should the following boolean be true, the // throw is called. In this case an object of the // DivideByZeroError class is defined. While the // name of this object is not specified, the // contents of the object, namely the error message // is passed to the object in the argument of the // catch(DivideByZero) instance. This object therefore // prints in the called catch(); // if (num2==0) throw DivideByZeroError(); return (float) num1/ num2; } int main() { int numb1, numb2; cout << "Enter the dividend of the quotient: "; cin >> numb1; cout << "Enter the divisor of the quotient: "; cin >> numb2; try { float result = quotient(numb1, numb2); cout << "The quotient is: " << result << endl; } // If the try block terminates correctly, the following // catch is not called and program execution begins after // it. // // Notice that the program will return a 1 to the OS if // the program terminated by a zero division but it // returns a 0 if the program terminates normally. // catch(DivideByZeroError error) { cout << "Error "; error.printMessage(); cout << endl; return 1; } char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; return 0; } // program_id except3.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a value for which // there is no catch. In this case // the program is aborted. // #include<iostream> using namespace std; void main() { cout << "This is the start." << endl; try { cout << "Got into the try block." << endl; throw 34; cout << "This line of code is not reached." << endl; } catch(char i) { cout << "The program throw char is " << i << endl; } cout << "The program will now end." << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except4.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a variable. It has // several calls to the function // but not all execute the throw. // // #include<iostream> using namespace std; void MYTEST(int value) { cout << "Inside of MYTEST. The value is: " << value << endl; if(value) throw value; } void main() { cout << "Start of main() " << endl; try { cout << "Inside of try block" << endl; MYTEST(0); MYTEST(1); MYTEST(2); } catch(int value) { cout << "Caught the value " << value << endl; } cout << "This is the end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except5.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // throw of a variable which could // be any data type or a string. // #include<iostream> using namespace std; void MYTEST(int value) { try { if(value) throw value; else throw "The Value is zero"; } catch(int value) { cout << "Caught the value " << value << endl; } catch(char *message) { cout << "The message is: " << message << endl; } } void main() { cout << "Start of main() " << endl; MYTEST(1); MYTEST(2); MYTEST(0); MYTEST(3); cout << "The successful end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except6.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // use of a default catch() which // catches all throws. // // // #include<iostream> using namespace std; void MYTEST(int value) { try { if(value==1) throw value; if(value==2) throw "Value is zero"; if(value==0) throw 452.22; } catch(…) { cout << "Caught some value "<< endl; } } void main() { cout << "Start of main() " << endl; MYTEST(1); MYTEST(2); MYTEST(0); cout << "The end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except7.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // default catch() but as only // one of several alternatives. // This is a better approach than // except6.cpp where the default // caught everything. // #include<iostream> using namespace std; void MYTEST(int value) { try { if(value==1) throw value; if(value==2) throw "Value is zero"; if(value==0) throw 452.22f; } catch(float value) { cout << "Caught a float of value " << value << endl; } catch(int value) { cout << "Caught an int of value " << value << endl; } catch(…) { cout << "Caught some value "<< endl; } } void main() { cout << "Start of main() " << endl; MYTEST(1); MYTEST(2); MYTEST(0); cout << "The end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except8.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates the // restriction on a function as to // the type of throws that can // occur. What is supposed to happen here, // is that only int and char values may be // thrown. However, you will notice that // this program also processes a double // in Microsoft Visual Studio. In addition // an warning occurs stating that this // is a disallowed feature of Visual Studio. // // #include<iostream> using namespace std; void MYTEST(int value) throw(int, char) { try { if(value==1) throw value; if(value==2) throw 'V'; if(value==0) throw 452.22; } catch(char value) { cout << "Caught a char of value: " << value << endl; } catch(int value) { cout << "Caught an int of value: " << value << endl; } catch(…) { cout << "Caught a value not on the throw list " << endl; } } void main() { cout << "Start of main() " << endl; MYTEST(1); MYTEST(2); MYTEST(0); cout << "The end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id except9.cpp // author don voils // date written 11/25/2006 // // description This program demonstrates that // a catch() may contain a throw // as well as a try block and // that a throw does not need // to have an argument. What // happened to the first throw // value? // // #include<iostream> using namespace std; void MYTEST() { try { throw "USA"; } catch(char* value) { cout << "Caught " << value << " inside MYTEST." << endl; throw; } } void main() { cout << "Start of main() " << endl; try { MYTEST(); } catch(char* value) { cout << "Inside of main() " << endl; cout << "Caught " << value << " inside of main() from a throw by MYTEST()"<< endl; } catch(…) { cout << "Not a string" << endl; } cout << "The end of main() " << endl; char resp; cout << endl << endl << "Continue? "; cin >> resp; cout << endl << endl; } // program_id fractions.cpp // written_by don voils // date_written 12/1/2006 // // description This program demonstrates // how a class can be made robust // by using exceptions. // #include<iostream> using namespace std; class Fractions { private: long top; long bottom; public: Fractions() { top = 0; bottom = 1; } Fractions(long t, long b) { if(b==0) { throw true; } top = t; bottom = b; reduce(); } void setTop(long t) { top = t; } void setBottom(long b) { if(b==0) { throw true; } bottom = b; } long getTop() { return top; } long getBottom() { return bottom; } Fractions operator+(Fractions theRight) { Fractions temp; temp.top = top*theRight.bottom + bottom*theRight.top; temp.bottom = bottom * theRight.bottom; temp.reduce(); return temp; } Fractions operator-(Fractions theRight) { Fractions temp; temp.top = top*theRight.bottom - bottom*theRight.top; temp.bottom = bottom * theRight.bottom; temp.reduce(); return temp; } Fractions operator*(Fractions theRight) { Fractions temp; temp.top = top*theRight.top; temp.bottom = bottom*theRight.bottom; temp.reduce(); return temp; } Fractions operator /(Fractions theRight) { Fractions temp; if(theRight.top==0) throw true; temp.top = top * theRight.bottom; temp.bottom = bottom * theRight.top; temp.reduce(); return temp; } long newGcd(long numb1, long numb2) { return (numb1<0) ? newGcd(-numb1,numb2):((numb2==0)? numb1:newGcd(numb2,numb1%numb2)); } void reduce() { long gcd = newGcd(top,bottom); top = top/gcd; bottom = bottom/gcd; } void show() { cout << " " << top << '/' << bottom << " "; } friend ostream &operator << (ostream &stream, Fractions ab); friend istream &operator >> (istream &stream, Fractions &ab); }; ostream &operator << (ostream &stream, Fractions ab) { stream << " " << ab.top << "/"; stream << ab.bottom << " "; return stream; } istream &operator >> (istream &stream, Fractions &ab) { char slash; stream >> ab.top >> slash >> ab.bottom; if(ab.bottom==0) throw true; ab.reduce(); return stream; } void main() { bool again = false; Fractions theFirst, theSecond; do { again = false; cout << endl << "Enter the first fraction? "; try { cin >> theFirst; } catch(bool value) { if(value) { cout << endl << "You must enter a non-zero number for" << " the bottom of the fraction." << endl; again = true; } } }while(again); do { again = false; cout << endl << "Enter the second fraction? "; try { cin >> theSecond; } catch(bool value) { if(value) { cout << endl << "You must enter a non-zero number for" << " the bottom of the fraction." << endl; again = true; } } }while(again); Fractions theSum = theFirst + theSecond; cout << endl; cout << theFirst; cout << " + "; cout << theSecond; cout << " = "; cout << theSum << endl; Fractions theDifference = theFirst - theSecond; cout << endl; cout << theFirst; cout << " - "; cout << theSecond; cout << " = "; cout << theDifference << endl; Fractions theProduct = theFirst * theSecond; cout << endl; cout << theFirst; cout << " * "; cout << theSecond; cout << " = "; cout << theProduct << endl; try { if(theSecond.getTop()==0) throw true; Fractions theQuotient = theFirst / theSecond; cout << endl; cout << theFirst; cout << " / "; cout << theSecond; cout << " = "; cout << theQuotient; } catch(bool value) { if(value) cout << endl << "The top of the second fraction must be " << " non-zero. " << endl; } cout << endl << endl; } // program_id ratio2.cpp // author don voils // date written 11/25/2006 // // description This program is similar to // ratio.cpp except the catch( ) // functions have been switched. // In this one the first catch( ) // is for the derived class and // the second is for the base // class. Does it make a difference? // // // #include<iostream> using namespace std; class Ratio { private: int top, bottom; public: void setRatio(int t, int b) { top = t; bottom=b; } int showTop() { return top; } int showBottom() { return bottom; } }; class NewRatio : public Ratio { public: NewRatio operator *(NewRatio ratio1) { NewRatio temp; int a = (showTop())*(ratio1.showTop()); int b = (showBottom())*(ratio1.showBottom()); temp.setRatio(a,b); return temp; } }; void classHandler(int value) { Ratio a; a.setRatio(3,4); NewRatio b; b.setRatio(5,6); try { if(value==0) throw a; if(value==1) throw b; } catch(NewRatio b) { cout << "The NewRatio object is " << b.showTop() << '/' << b.showBottom() << endl; } catch(Ratio a) { cout << "The Ratio object is " << a.showTop() << '/' << a.showBottom() << endl; } } void main() { cout << "At the start of main()" << endl; classHandler(0); classHandler(1); cout << "At the end of main()" << endl; } // program_id ratio.cpp // author don voils // date written 11/25/2006 // // description This program is similar to // ratio2.cpp except the catch( ) // functions have been switched. // In this one the first catch( ) // is for the base class and // the second is for the derived // class. Does it make a difference? // // // #include<iostream> using namespace std; class Ratio { private: int top, bottom; public: void setRatio(int t, int b) { top = t; bottom=b; } int showTop() { return top; } int showBottom() { return bottom; } }; class NewRatio : public Ratio { public: NewRatio operator *(NewRatio ratio1) { NewRatio temp; int a = (showTop())*(ratio1.showTop()); int b = (showBottom())*(ratio1.showBottom()); temp.setRatio(a,b); return temp; } }; void classHandler(int value) { Ratio a; a.setRatio(3,4); NewRatio b; b.setRatio(5,6); try { if(value==0) throw a; if(value==1) throw b; } catch(Ratio a) { cout << "The ratio object is " << a.showTop() << '/' << a.showBottom() << endl; } catch(NewRatio b) { cout << "The new_ratio object is " << b.showTop() << '/' << b.showBottom() << endl; } } void main() { cout << "At the start of main()" << endl; classHandler(0); classHandler(1); cout << "At the end of main()" << endl; }