Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which shows that the order of exception handlers is important. T

ID: 3769401 • Letter: W

Question

Write a program which shows that the order of exception handlers is important. The first matching handler is the one thatexecutes. Compile and run your program two different ways to show that two different handlers execute with two different effects.

Hello, I want to write a program to the above description. I wrote a division problem program that throws an exception for a dividend of 0, a dividend that is even (I only want to divid by odd numbers for this example) and if the user inputs a non int. I am getting an error ("error: no opperator '<<' matches these operands). What am I doing wrong as i try to handle the exception for even numbers? Thank you very much for your help.

#include <iostream>

using namespace std;

int main()
{
   int dividend;
   int divisor = 1;
   int quotient;

   string s1 = "Not a valid input number.";

     
   cout << "Perform a division problem(with a catch). Do not divide by even numbers or negative numbers. ";

   try{
      
       cout << "Enter Dividend: ";
       cin >> dividend;
       cout << endl;

       cout << "Enter Divisor: ";
       cin >> divisor;
       cout << endl;

       if (divisor == 0)
       {
           throw divisor;
       }

       else if (divisor % 2 == 0)
       {
           throw string ("Even numbers are not allowed.");
       }
       else if (!cin)
       {
           throw s1;
       }

         
            
       quotient = dividend / divisor;
       cout << "Quotient is equal to " << quotient << endl;
   }
   catch (int x)
     
   {
       cout << "Error: Division by " << x << endl;
     
     
   }
   catch (string s)
   {
       cout << "Error: " << s << endl;
   }
  


system("pause");
return 0;

}

Explanation / Answer

I executed the program . you need to add cstdlib. When I enter non int, the output gives error divied by 0. I changed code to differentiate b/w even and negative number divisor.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
     int dividend;
     int divisor = 1;
     int quotient;

     string s1 = "Not a valid input number.";


     cout << "Perform a division problem(with a catch). Do not divide by even numbers or negative numbers. ";

     try{

         cout << "Enter Dividend: ";
         cin >> dividend;
         cout << endl;

         cout << "Enter Divisor: ";
         cin >> divisor;
         cout << endl;

         if (divisor == 0)
         {
            throw divisor;
         }

         else if (divisor % 2 == 0)
         {
           if(divisor > 0)
               throw string ("Even numbers are not allowed.");
           else
               throw string ("Negative numbers are not allowed.");
         }
         else if (!cin)
         {
            throw s1;
         }



         quotient = dividend / divisor;
         cout << "Quotient is equal to " << quotient << endl;
     }
     catch (int x)

     {
        cout << "Error: Division by " << x << endl;


     }
     catch (string s)
     {
        cout << "Error: " << s << endl;
     }



system("pause");
return 0;

}

-------------------output------------------------

Perform a division problem(with a catch).
Do not divide by even numbers or negative numbers.

Enter Dividend: 6

Enter Divisor: 2

Error: Even numbers are not allowed.
Press any key to continue . . .

Perform a division problem(with a catch).
Do not divide by even numbers or negative numbers.

Enter Dividend: 5

Enter Divisor: dd

Error: Division by 0
Press any key to continue . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote