Answer to Question #269180 in C++ for Amit

Question #269180

WAP to show exception using try throw and catch block.


1
Expert's answer
2021-11-21T04:51:16-0500


SOLUTION TO THE ABOVE QUESTION


SOLUTION CODE


#include <iostream>

using namespace std;


int main()

{

   int my_number = -5;

   cout << "Before checking exception using try";

    //Writing the try block
   try {


      cout << "This is the try block \n";


      if (my_number < 0)


      {


         throw my_number ;


         cout << "This statement will never execute if an exception is thown\n";


      }


   }

    //Writing the catch block
   catch (int my_number ) {


      cout << "Exception Caught here \n";


   }

 

   cout << "This statement executes after catch\n";


   return 0;

}









Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS