1. Error in running stage
1) The program opens an unavailable File
2) Excessive request memory
3) unacceptable values
2. call abort () to terminate a process with an exception # include <stdlib. h>
Process: 1) send the message Abnormal Program termination to the standard error stream
2) Terminate the program
3) return a value that varies with the implementation, telling the OS or parent process that the processing failed.
#include <cstdlib>int main(){abort();return 0;}
Iii. Exception Mechanism
1) Example:
#include <iostream>using namespace std;double hmean(double a,double b){if(a == -b)throw "bad hmean() a=-b";return 2*a*b/(a+b);}int main(){double x,y,z;cout<<"Enter two numbers: ";while(cin>>x>>y){try{z=hmean(x,y);}catch(const char *s){cout<<s<<endl;cout<<"Enter next set of numbers :";continue;}cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<endl;cout<<"Enter next pair of numbers: <q to quit>";}cout<<"Bye!\n";return 0;}
Note: throw is similar to executing a Return Statement, which terminates the execution of the function. Throw does not return control to the calling program, but causes the program to lag behind the function call sequence. Until the try block function is found.
2) exception class
Catch all exceptions: Catch (...){}
Precautions for exceptions: 1> program code is added when exceptions are used.
2> reduce program running speed
3> the exception specification is not applicable to the template. The exceptions caused by the template vary with the specific specifics.
Iii. rtti (runtime type identification) stage type identification
1) Meaning: The program can use pointers or references of the base class to check the actual derived types of these pointers or referenced objects.
2) rtti provides the following two very useful operators:
1) The typeid operator returns the pointer and the actual type referred to by the reference. typeid (a). Name () can know the type of variable.
2) The dynamic_cast operator securely converts a pointer or reference of the base class type to a pointer or reference of the derived type.
3) Example:
# Include <iostream> # include <cstdlib> # include <ctime> using namespace STD; Class grand {PRIVATE: int hold; public: Grand (INT h = 0 ): hold (h) {} virtual void speak () const {cout <"I Am a grand class! \ N ";}virtual int value () const {return hold ;}}; class superb: Public grand {public: superb (INT h = 0): Grand (h) {} void speak () const {cout <"I am a super class! \ N ";} virtual void say () const {cout <" I hold the superb Value of "<value () <"! \ N ";}}; class magnificent: Public superb {PRIVATE: Char ch; public: Magnificent (INT h = 0, char CV = 'A'): superb (H ), CH (CV) {} void speak () const {cout <"I Am a magnificent class! \ N ";} virtual void say () const {cout <" I hold the character "<ch <" and the interger "<value () <"! \ N ";}}; grand * getone () {grand * P; Switch (RAND () % 3) {Case 0: P = new grand (RAND () % 100); break; Case 1: P = new superb (RAND () % 100); break; Case 2: P = new magnificent (RAND () % 100, 'A' + rand () % 26); break;} return P;} int main () {srand (time (0); Grand * PG; superb * pS; for (INT I = 0; I <5; I ++) {Pg = getone (); cout <"now processing type" <typeid (* PG ). name () <". \ n "; // typeid (* PG ). name () returns the class name PG-> speak (); If (PS = dynamic_cast <superb *> (PG) // dynamic_cast <superb *> (PG) safely convert the pointer or reference of the base class type to a pointer of the derived type or reference PS-> say (); If (typeid (magnificent) = typeid (* PG )) cout <"Yes, you are really magnificent. \ n ";}return 0 ;}
Output: (the running result varies with the random number)