Exception Handling
- What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class ExceptionExample: public exception
{
virtual const char* what() const throw()
{
return "Exception Occurred...";
}
} NewExcep;
int main ()
{
try
{
throw NewExcep;
}
catch (exception& excep)
{
cout << excep.what() << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
In this program, We are arising a standard exception and catching that and returning a statement.
- What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int a = 8;
try
{
if (a < 0)
throw "Positive Number Required";
cout << a << "\n\n";
}
catch(const char* Msg)
{
cout << "Error: " << Msg;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
In this program, We are checking the age of a person, If it is zero means, We will arise a exception.
- What is the output of this program?
#include <iostream>
using namespace std;
double DivFunction(int num1, int num2)
{
if ( num2 == 0 )
{
throw "Division by zero condition Occurred...";
}
return (num1 / num2);
}
int main ()
{
int m = 10;
int n = 0;
double Res = 0;
try
{
Res = DivFunction(m, n);
cout << Res << endl;
}
catch (const char* msg)
{
cout << msg << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
We are dividing the values and if one of the values is zero means, We are arising an exception.
- What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
void Function()
{
cout << "Unexpected handler called\n";
throw;
}
void NewFunction () throw (int,bad_exception)
{
throw 'U';
}
int main (void)
{
set_unexpected (Function);
try
{
NewFunction();
}
catch (int)
{
cout << "caught int\n";
}
catch (bad_exception be)
{
cout << "Caught Bad_exception\n";
}
catch (...)
{
cout << "Caught other exception \n";
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
In this program, We are calling set_unexpected and NewFunction, So it is printing the output.
- What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
int num = -2;
try
{
if (num < 0)
{
throw num;
}
else
{
cout<< num;
}
}
catch (int num )
{
cout << "Exception occurred: Thrown value is " << num << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
As the given value is -2 and according to the condition, We are arising an exception.