Exception Handling
- What is the main purpose of the constructor?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
The purpose of a constructor is to establish the class invariant. To do that, it often needs to acquire system resources or in general perform an operation that may fail.
- Why is it expensive to use objects for the exception?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
If an error occurs in the program, then only exception object is created otherwise, It will not be created. So it’s expensive to use in the program.
- What is the output of this program?
#include <iostream>
#include <exception>
using namespace std;
int main()
{
try
{
double* number= new double[200];
cout << "Memory allocated...";
}
catch (exception& excep)
{
cout << "Exception occurred:" << excep.what() << endl;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
The value will be allocated, if there is enough memory in the system.
- What is the output of this program?
#include <iostream>
using namespace std;
void Example(int n)
{
try
{
if (n > 0)
throw n;
else
throw 'L';
}
catch(char)
{
cout << "Catch a integer and that integer is :" << n;
}
}
int main()
{
cout << "Testing multiple catches :";
Example(5);
Example(0);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
As the catch is created with a wrong type, So it will arise a runtime error.
- What is the output of this program?
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void Function(int ch)
{
if (ch < numeric_limits:: max())
throw invalid_argument("Function argument too large.");
else
{
cout<<"Programs Executed...";
}
}
int main()
{
try
{
Function(150);
}
catch(invalid_argument& excep)
{
cout << excep.what() << endl;
return -1;
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
As we are throwing the function and catching it with a correct data type, So this program will execute.