C++ Programming
ERROR HANDLING MCQs
Total Questions : 10
bad_cast exception is thrown by dynamic_cast.
None.
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
struct MyException : public exception
5.
{
6.
const char * what () const throw ()
7.
{
8.
return "C++ Exception";
9.
}
10.
};
11.
int main()
12.
{
13.
try
14.
{
15.
throw MyException();
16.
}
17.
catch(MyException& e)
18.
{
19.
cout
We are defining the user-defined exception in this program.
Output:
$ g++ excep4.cpp
$ a.out
C++ Exception
Exception caught
In this program, We are allocating the memory for array. If it is allocated means, no exception
will arise and if there is no size in memory means, Exception will arise.
Output:
$ g++ excep3.cpp
$ a.out
allocated
What is the output of this program?
1.
#include
2.
#include
3.
using namespace std;
4.
class myexception: public exception
5.
{
6.
virtual const char* what() const throw()
7.
{
8.
return "My exception";
9.
}
10.
} myex;
11.
int main ()
12.
{
13.
try
14.
{
15.
throw myex;
16.
}
17.
catch (exception& e)
18.
{
19.
cout
This is a standard exception handler used in the class.
Output:
$ g++ excep2.cpp
$ a.out
My exception
We are handling the exception by throwing that number. So the output is printed with the given number.
Output:
$ g++ excep1.cpp
$ a.out
An exception occurred 20
What is the output of this program?
1.
#include
2.
using namespace std;
3.
double division(int a, int b)
4.
{
5.
if (b == 0)
6.
{
7.
throw "Division by zero condition!";
8.
}
9.
return (a / b);
10.
}
11.
int main ()
12.
{
13.
int x = 50;
14.
int y = 0;
15.
double z = 0;
16.
try
17.
{
18.
z = division(x, y);
19.
cout
It's a mathematical certainty, We can't divide by zero, So we're arising a exception.
Output:
$ g++ excep.cpp
$ a.out
Division by zero condition!
finally keyword will be executed at the end of all the exception.
When we found a exception in the program, We need to throw that and we handle that by using
Recent Questions
Q. In Submerged Arc Welding, An Arc Is Produced Between A
Q. The Dinesh Goswami Committee Recommended
Q. Which Command Is Used To Remove A Directory?
Q. Least Accurate Instrument For Measuring Volume Is
Q. Sometimes The Overhead Of Keeping Track Of A Hole Might Be :....
Q. The Rectifier Instrument Is Not Free From
Q. The Two Links OA And OB Are Connected By A Pin Joint At O. I....
Q. Which Of The Following Has Lower Sp. Gravity V
Q. Which Of The Following Best Describes The Aim Of Sustainable....
Q. LOST
Q. Lance Armstrong, A Sportsperson Of International Repute, Bel....
Q. In A 3 Phase Full Converter The Average Load Current Is 150 ....
Q. Intracellular Structures Formed During Many Viral Infections....
Q. Look At This Series: 1,000, 200, 40, . . . What Number Shoul....
Q. Find Out Whether There Is Any Grammatical Error In Below Sen....
Q. The Oath Of Office Is Administered To The Members Of State C....
Q. Higher CO₂ Concentration Stimulates:
Q. The Latest Release Of The OBIEE Was In
Q. An Interpreter Is
Q. If Sodium (Na) Is Burned In Limited Supply Of Oxygen (O₂),....