Exception Handling in C++ : Programs

********************************PROGRAM 1*****************************

#include<iostream>
 using namespace std;

int main ()
 {
 int num1,num2,num3; float res;
 cout<<"enter 3 numbers \n";
 cin>>num1>>num2>>num3;
 try
 {
 if((num2-num3)==0)
 throw 0;
 else
 res=num1/float(num2-num3);
 }
 catch(int i)
 {
 cout<<"Cant't divide by zero.";
 res=num1;
 }
 cout<<"\nresult is "<<res;
 return 0;
 }

********************************PROGRAM 2*****************************

//Demonstration of multiple catch block with generalized/global catch block
 #include<iostream>
 using namespace std;
 int main ()
 {
 cout<<"Demonstration of multiple catch\nEnter your age\n";
 int num;
 cin>>num;
 try
 {
 if(num<17)
 throw num;
 else if(num>=17 && num<=22)
 cout<<"You are eligible to attend class";
 else if(num > 22 && num <=28)
 throw 0.0f;
 else if(num>28 && num<35)
 throw 'c';
 else
 throw "error";
 }
 catch(int a)
 {
 cout<<"You are not eligible for class "<<a;
 }
 catch(float f)
 {
 cout<<"You must complete class as soon as possible";
 }
 catch(char c)
 {
 cout<<"Upgrade for higher course. This is basic course not intended for you";
 }
 catch(...)
 {
 cout<<"Sorry ! We dont have any course for you";
 }
 return 0;
 }

********************************PROGRAM 3*****************************

//Program to demonstrate array index out of bound exception
 #include<iostream>
 using namespace std;
 int main ()
 {
 int a[10],k;
 try
 {
 cout<<"enter number of elements in array\n";
 cin>>k;
 if(k>10)
 throw k;
 }
 catch(int i)
 {
 cout<<"Array cant store "<<k<<" elements\n";
 k=10;
 }
 cout<<"Enter elements in array\n";
 for(int i=0;i<k;i++)
 {
 cin>>a[i];
 }
 for(int j=0;j<k;j++)
 cout<<a[j]<<"\t";
 return 0;
 }

********************************PROGRAM 4*****************************

//Function throwing exception

#include<iostream>
 using namespace std;

float divide(int n1,int n2)
 {
 float res;
 if(n2==0)
 throw 0;
 else
 {
 res=n1/(float)n2;
 return res;
 }
 }
 int main ()
 {
 int num1,num2;
 float res;
 cout<<"enter 2 numbers \n";
 cin>>num1>>num2;
 try
 {
 res=divide(num1,num2);
 }
 catch(int)
 {
 res=num1;
 }
 cout<<"\nresult is "<<res;
 return 0;
 }

********************************PROGRAM 5*****************************

#include<iostream>
 using namespace std;

float divide(float n1,float n2) throw (char,float)
 {
 float res;
 if(n2==0.0f)
 throw '0';
 else if (n2<0.0f)
 throw 0.0f;
 else
 {
 res=n1/n2;
 }
 return res;
 }
 int main ()
 {
 float num1,num2,res;
 cout<<"enter 2 numbers \n";
 cin>>num1>>num2;
 try
 {
 res=divide(num1,num2);
 }
 catch(char)
 {
 res=0.0f;
 }
 cout<<"\nresult is "<<res;
 return 0;
 }

********************************PROGRAM 6*****************************

//Rethrowing exception

#include<iostream>
 using namespace std;

float divide(int n1,int n2)
 {
 float res;
 try
 {
 if(n2==0)
 throw 0;
 else
 {
 res=n1/(float)n2;
 return res;
 }
 }
 catch(int)
 {
 cout<<"Rethrowing to calling function";
 throw;
 }
 }
 int main ()
 {
 int num1,num2;
 float res;
 cout<<"enter 2 numbers \n";
 cin>>num1>>num2;
 try
 {
 res=divide(num1,num2);
 }
 catch(int)
 {
 res=num1;

}
 cout<<"\nresult is "<<res;
 return 0;
 }

********************************PROGRAM 7*****************************

//User defined Exception class
 #include<iostream>
 using namespace std;
 class Error
 {
 int code;
 string err;
 public:
 Error(int c, string e)
 {
 code=c;
 err=e;
 }
 void err_disp()
 {
 cout<<"\nError :\t"<<code;
 cout<<"\nError Message :\t"<<err;
 }
 };
 int main()
 {
 try
 {
 cout<<"Enter your age";
 int age;
 cin>>age;
 if(age<0)
 throw Error(age,"Age cant be negative");
 else
 cout<<"\nYour age is\t "<<age;

}
 catch(Error e)
 {
 e.err_disp();
 }
 return 0;
 }

********************************PROGRAM 8*****************************

//User Defined Exception class
 #include <iostream>
 #include <exception>
 using namespace std;
 class Divide_By_Zero_Exception : public exception
 {
 public:
 const char * what() const throw()
 {
 return "Divide By Zero Exception\n";
 }
 };
 int main()
 {
 try
 {
 int a, b;
 cout << "Enter two numbers : ";
 cin >> a >> b;
 // compute a / b
 if (b == 0)
 {
 Divide_By_Zero_Exception d;
 throw d;
 }
 else
 {
 cout << "a / b = " << a/b << endl;
 }
 }
 catch(exception& e)
 {
 cout << e.what();

}
 }

********************************PROGRAM 9*****************************

//Stack unwinding demonstration in Exception handling
 #include <iostream>
 using namespace std;
 void fun1() throw (int)
 {
 cout<<"\nFunciton 1 starts";
 throw 10;
 cout<<"\nrfunction 1 ends";
 }
 void fun2() throw (int)
 {
 cout<<"\nFunciton 2 starts";
 fun1();
 cout<<"\nrfunction 2 ends";
 }
 void fun3()
 {
 cout<<"\nFunciton 3 starts";
 try
 {
 fun2();
 }
 catch(int)
 {
 cout<<"\nInside catch block";
 }
 cout<<"\nFunction 3 ends";
 }

int main() {
 fun3();
 return 0;
 }

********************************PROGRAM 10*****************************

//Exception in Inheritance
 #include <iostream>
 using namespace std;
 class base
 {
 private:
 int age;
 string name;
 public:
 void getData()
 {
 cout<<"Enter name and age\n";
 cin>>name>>age;
 if(age<0)
 throw base();
 }
 void showData()
 {
 cout<<"Name is :"<<name<<endl;
 cout<<"Age is :"<<age<<endl;
 }
 };
 class derived:public base
 {
 private:
 int rollno,grno;
 public:
 void getData1()
 {
 //base::getData();
 cout<<"Enter roll no and gr no\n";
 cin>>rollno>>grno;
 if(rollno<0)
 throw derived();
 }
 void showData1()
 {
 //base::showData();
 cout<<"Roll No is :"<<rollno<<endl;
 cout<<"GR no is :"<<grno<<endl;
 }

};
 int main()
 {
 try
 {
 derived d;
 d.getData();
 d.getData1();
 d.showData();
 d.showData1();
 }
 catch(derived)
 {
 cout<<"\nIn catch block of derived class\n";
 }
 catch(base)
 {
 cout<<"\nIn catch block of Base class\n";
 }

return 0;
 }