Exception Handling
- When exceptions are used?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
Exceptions are used when postconditions of a function can be satisfied
- What is the output of this program?
#include <iostream>
using namespace std;
double Div(int num1, int num2)
{
if (num2 == 0)
{
throw "Division by zero condition!";
}
return (num1 / num2);
}
int main ()
{
int p = 40;
int q = 0;
double R = 0;
try
{
R = Div(p, q);
cout << R << endl;
}
catch (const char* Message)
{
cerr << Message << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
NA
- What is the output of this program?
#include <iostream>
using namespace std;
int main ()
{
try
{
throw 15;
}
catch (int e)
{
cout << "Exception occurred..." << e << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
We are handling the exception by throwing that number. So the output is printed with the given number.
- What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
class NewException: public exception
{
virtual const char* what() const throw()
{
return "New Exception occurred...";
}
} NewExcep;
int main ()
{
try
{
throw NewExcep;
}
catch (exception& e)
{
cout << e.what() << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
This is a standard exception handler used in the class.
- What is the output of this program?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void Function(char ch)
{
if (ch < numeric_limits::max())
return invalid_argument;
}
int main()
{
try
{
Function(50);
}
catch(invalid_argument& excep)
{
cout << excep.what() << endl;
return -1;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
We can’t return a statement by using the return keyword, So it is arising an error.
Compilation ErrorIn function 'void MyFunc(char)':
8:36: error: expected primary-expression before ';' token
8:36: error: return-statement with a value, in function returning 'void' [-fpermissive]
In function 'int main()':
14:23: warning: overflow in implicit constant conversion [-Woverflow]