13.1

Write a program containing a possible exception. Use a try block to throw it and a catch block to handle it promptly.

Answer
#include<iostream>
#include<math>
#define pi 3.1416
using namespace std;
void power_factor(float a)
{
                       if(a>1 || a<-1)
                         throw(a);
                           else
         cout<<"Voltage(V)is lagging from current(I) by "<<acos(a)*180/pi<<" degree\n";
}

int main()
{
            float a;
              try
               {
                        cout<<" Enter power factor ";
                           cin>>a;
                             power_factor(a);
                 }
                 catch(float b)
                 {
                         cout<<" Caught an exception \n";
                   }
             return 0;
  }

OUTPUT

Enter power factor 2

Caught an exception
13.2

Write a program that illustrates the application of multiple catch statements.

#include<iostream>
#define size 5
using namespace std;
void multiple_catch(int n)
{
            float v[size];
               try
                 {
                             if(n>size)
                               throw(n);
                                else if(n<=0)
                                throw(0.01);
                                else
                                  {
                                         cout<<" Enter "<<n<<" elements one by one \n";
	 for(int i=0;i<n;i++)
	     cin>>v[i];
	 cout<<" Now contents  of v["<<n<<"]: \n";
					                      for(i=0;i<n;i++)
	            cout<<v[i]<<"   ";
                                      }
                        }
                        catch(int m)
                         {
                               cout<<" array size must be less than or equal  "<<size<<" \n";
                          }
                     catch(double c)
                    {
                             cout<<" array size must be positive except 0 \n";
                     }
}


int main()
{
        int s;
        cout<<" How many elements do you want to enter ? ";
          cin>>s;
          multiple_catch(s);
        return 0;
}

OUTPUT

How many elements do you want to enter ? 20

array size must be less than or equal 5
13.3

Write a program which uses catch(…) handler.

Answer
#include<iostream>
#define size 5
using namespace std;
void multiple_catch(int n)
{
           float v[size];
              try
               {
                           if(n>size)
                              throw(n);
                              else if(n<=0)
                              throw(0.01);
                           else
                               {
                                  cout<<" Enter "<<n<<" elements one by one \n";
	 for(int i=0;i<n;i++)
	     cin>>v[i];
                                     cout<<" Now contents  of v["<<n<<"]: \n";
					                   for(i=0;i<n;i++)
                                         cout<<v[i]<<"   ";
                                  }
                    }
                 
               catch(...)
                  {
                         cout<<" an exception is detected \n";
                   }
}

int main()
{
                    int s;
                  cout<<" How many elements do you want to enter ? ";
                  cin>>s;
                  multiple_catch(s);
                  return 0;
}

OUTPUT

How many elements do you want to enter ?  10

an exception is detected
13.4

Write a program that demonstrates how certain exception types are not allowed to be thrown.

Answer
#include<iostream>
using namespace std;
void empty() throw()
{
	  cout<<"In empty()\n";
}

void with_type( float x) throw(int)
{

	if(x==1)
	throw(1);
	else if(x==1.1)
	throw (2.1);
}

int main()
{
		try
		{
				empty();
				with_type(1);
		}
		catch (int n)
		{
				cout<<"Caught an int  = "<<n;
		}
		catch(float)
		{
			cout<<"Caught a float ";
		}
		return 0;
}

OUTPUT

In empty()

Caught an int  = 1
14.5

Write a program to demonstrate the concept of re-throwing an exception.

Answer
#include<iostream>
using namespace std:
void division(int a,int b)
{
       try
          {
                       if(b==0)
                        throw b;
                         else 
                         cout<<" a/b = "<<(float)a/b<<"\n";
             }


 
          catch(int)
             {
                          cout<<" Caught an exception  as first throwing \n";
                                 throw;
               }
}
int main()
{
               int a,b;
    
               cout<<" Enter the value of a & b : ";
               cin>>a>>b;
               try
                   {
                            division(a,b);
                    }
                 catch(int)
                  {
                            cout<<" Caught an exception as rethrowing \n";
                   }
	
             return 0;
}

OUTPUT

Enter the value of a & b :  200   0

Caught an exception as first throwing
Caught an exception as rethrowing
13.6 Write a program with the following:
  • (a) a function to read two double type numbers from keyboard.
  • (b) a function to calculate the division of these two numbers.
  • (c) a try block to throw an exception when a wrong type of data is keyed in.
  • (d) A try block to detect and throw an exception if the condition “divide by zero” occurs.
  • (e) Appropriate catch block to handle the exception thrown.
Answer
#include<iostream>
using namespace std;
class santo
{
               double a;
               double b;
             public:
              void input(double x,double y){a=x;b=y;}
              void division();
};

void santo::division()
{
        try
        {
                  if(b==0)
                  throw b;
                  else
                   cout<<" a/b = "<<a/b<<"\n";
           }
          catch(int)
           {
                    cout<<" an exception is caught \n";
           }
}

int main()
{
                double d;
                 int m;
                  santo hasibul;
                 double m1,n1;
                cout<<"Enter two number : ";
                  cin>>m>>n1;
                    


                  try
                      {
                               if(sizeof(d)!=sizeof(m) || sizeof(d)!=sizeof(n1))
                                 throw n1;
                                 else
                                      {
	     hasibul.input(m,n1);
	      hasibul.division();
                                       }

                          }
                   catch(double)
                      {
                                 cout<<" Caught an exception \n ";
                        }
              return 0;
}

OUTPUT

Enter two number :   150   0

Caught an exception
13.7

Write a main program that calls a deeply nested function containing an exception. Incorporate necessary exception handling mechanism.

Answer
#include<iostream>
using namespace std;
long int square(int i)
{
         return i*i;
}
long int sum(int n)
{
                long int s;
                s=0;
                for(int i=1;i<=n;i++)
                 {
                         s+=square(i);
                 }

           return s;

}

void display(int m)
{
                try
                 {
                             if(m<0)
                              throw m;
                               else
                               cout<<sum(m)<<"\n";
                   }
               catch(int n)
                  {
                             cout<<" Caught an exception \n ";
                  }

}

int main()
{
                int n;
                cout<<" Enter a positive number ";
                 cin>>n;
                 display(n);
                 return 0;
}

OUTPUT

Enter a positive number -23

Caught an exception

Next Previous