WAP to show exception using try throw and catch block.
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;
}
Comments
Leave a comment