C++ Exception Handling Interview Questions And Answers
Download C++ Exception Handling Interview Questions and Answers PDF
Sharpen your C++ Exception Handling interview expertise with our handpicked 29 questions. These questions will test your expertise and readiness for any C++ Exception Handling interview scenario. Ideal for candidates of all levels, this collection is a must-have for your study plan. Access the free PDF to get all 29 questions and give yourself the best chance of acing your C++ Exception Handling interview. This resource is perfect for thorough preparation and confidence building.
29 C++ Exception Handling Questions and Answers:
C++ Exception Handling Job Interview Questions Table of Contents:
1 :: Can you please Illustrate Rethrowing exceptions?
Rethrowing an expression from within an exception handler can be done by calling throw, by itself, with no exception. This causes current exception to be passed on to an outer try/catch sequence. An exception can only be rethrown from within a catch block. When an exception is rethrown, it is propagated outward to the next catch block.
Consider following code:
#include <iostream>
using namespace std;
void MyHandler()
{
try
{
throw “hello”;
}
catch (const char*)
{
cout <<”Caught exception inside MyHandler\n”;
throw; //rethrow char* out of function
}
}
int main()
{
cout<< “Main start”;
try
{
MyHandler();
}
catch(const char*)
{
cout <<”Caught exception inside Main\n”;
}
cout << “Main end”;
return 0;
}
O/p:
Main start
Caught exception inside MyHandler
Caught exception inside Main
Main end
Thus, exception rethrown by the catch block inside MyHandler() is caught inside main();
Read MoreConsider following code:
#include <iostream>
using namespace std;
void MyHandler()
{
try
{
throw “hello”;
}
catch (const char*)
{
cout <<”Caught exception inside MyHandler\n”;
throw; //rethrow char* out of function
}
}
int main()
{
cout<< “Main start”;
try
{
MyHandler();
}
catch(const char*)
{
cout <<”Caught exception inside Main\n”;
}
cout << “Main end”;
return 0;
}
O/p:
Main start
Caught exception inside MyHandler
Caught exception inside Main
Main end
Thus, exception rethrown by the catch block inside MyHandler() is caught inside main();
2 :: Explain Exception handling for a class with an example?
Exceptions are certain disastrous error conditions that occur during the execution of a program. They could be errors that cause the programs to fail or certain conditions that lead to errors. If these run time errors are not handled by the program, OS handles them and program terminates abruptly, which is not good.
To avoid this, C++ provides Exception Handling mechanism.
The three keywords for Exception Handling are:
Try, Catch and Throw.
The program tries to do something. If it encounters some problem, it throws an exception to another program block which catches the exception.
Ex:
void main()
{
int no1, no2;
try
{
cout << “Enter two nos:”;
cin >> no1 >> no2;
if (no2 == 0)
throw “Divide by zero”;
else
throw no1/no2;
}
catch (char *s)
{
cout << s;
}
catch (int ans)
{
cout << ans;
}
}
We know that divide by zero is an exception. If user enters second no as zero, the program throws an exception, which is caught and an error message is printed else the answer is printed.
Read MoreTo avoid this, C++ provides Exception Handling mechanism.
The three keywords for Exception Handling are:
Try, Catch and Throw.
The program tries to do something. If it encounters some problem, it throws an exception to another program block which catches the exception.
Ex:
void main()
{
int no1, no2;
try
{
cout << “Enter two nos:”;
cin >> no1 >> no2;
if (no2 == 0)
throw “Divide by zero”;
else
throw no1/no2;
}
catch (char *s)
{
cout << s;
}
catch (int ans)
{
cout << ans;
}
}
We know that divide by zero is an exception. If user enters second no as zero, the program throws an exception, which is caught and an error message is printed else the answer is printed.
3 :: Describe exceptions in C++?
Exceptions: Exceptions are certain disastrous error conditions that occur during the execution of a program. They could be errors that cause the programs to fail or certain conditions that lead to errors. If these run time errors are not handled by the program, OS handles them and program terminates abruptly, which is not good. So C++ provides Exception Handling mechanism.
Read More4 :: What is Synchronous Exceptions?
Errors that occur due to incorrect input data or incorrect handling of array indices (“out of range index”), memory overflow are known as Synchronous Exceptions
Read More5 :: What is Asynchronous Exceptions?
The errors that are caused by events that are beyond control of the program are Asynchronous Exceptions. E.g. Keyboard interrupts
Read More6 :: Explain benefits of Exception Handling?
The benefits of Exception Handling are:
1. Program is not terminated abruptly
2. User will understand what errors are occurring in the program.
The three keywords for Exception Handling are:
Try, Catch and Throw.
Read More1. Program is not terminated abruptly
2. User will understand what errors are occurring in the program.
The three keywords for Exception Handling are:
Try, Catch and Throw.
7 :: Explain terminate() function?
terminate() is a library function which by default aborts the program
It is called whenever the exception handling mechanism cannot find a handler for a thrown exception.
Read MoreIt is called whenever the exception handling mechanism cannot find a handler for a thrown exception.
8 :: Explain unexpected() function?
unexpected() is called when a function with an exception specification throws an exception of a type that is not listed in the exception specification for the function
A function declaration without a specification like throw(char*) may throw any type of exception, and one with throw() is not allowed to throw exceptions at all.
By default unexpected() calls terminate().
Read MoreA function declaration without a specification like throw(char*) may throw any type of exception, and one with throw() is not allowed to throw exceptions at all.
By default unexpected() calls terminate().
9 :: How to implement exception handling in C++?
Exception handling in C++ is implemented by using the try{} and catch(){} statements.
When a try block throws an exception, the program leaves the try block and enters the catch statement of the catch block.
If they type of the object thrown matches the arg type in the catch block, catch block is executed for handling the code.
If they are not caught, abort() function is executed by default.
When no exception is deteted or thrown then the control goes to the statement below the catch block.
Read MoreWhen a try block throws an exception, the program leaves the try block and enters the catch statement of the catch block.
If they type of the object thrown matches the arg type in the catch block, catch block is executed for handling the code.
If they are not caught, abort() function is executed by default.
When no exception is deteted or thrown then the control goes to the statement below the catch block.
10 :: An exception is thrown using _____________ keyword in CPP.
a. throws
b. throw
c. threw
d. thrown?
b. throw
Read More11 :: The code of statements which may cause abnormal termination of the program should be written under_________ block.
a. try
b. catch
c. Finally
d. None of these
a. try
Read More13 :: Exception handlers are declared with ____________ keyword.
a. Try
b. catch
c. throw
d. finally
b. catch
Read More15 :: Which of the following statements are true about Catch handler?
1. It must be placed immediately after try block T
2. It can have multiple parameters
3. There must be only one catch handler for every try block
4. There can be multiple catch handler for a try block T
5. Generic catch handler can be placed anywhere after try block.
a. Only 1, 4, 5
b. Only 1, 2, 3
c. Only 1, 4
d. Only 1, 2
c. Only 1, 4
Read More16 :: In nested try blocks, there is no need to specify catch handler for inner try block. Outer catch handler is sufficient for the program.
a. True
b. False
b. False
Read More17 :: In nested try block, if inner catch handler gets executed, then______________
a. Program execution stops immediately
b. Outer catch handler will also get executed
c. Compiler will jump to the outer catch handler and then executes remaining executable statements of main()
d. Compiler will execute remaining executable statements of outer try block and then the main()
d. Compiler will execute remaining executable statements of outer try block and then the main()
Read More18 :: Generic catch handler must be placed at the end of all the catch handlers.
a. True
b. False
a. True
Read More19 :: If inner catch handler is not able to handle the exception then__________.
a. Compiler will look for outer try handler
b. Program terminates abnormally
c. Compiler will check for appropriate catch handler of outer try block
d. None of these
c. Compiler will check for appropriate catch handler of outer try block
Read More20 :: In nested try blocks, if both inner and outer catch handlers are not able to handle the exception, then______________.
a. Compiler executes only executable statements of main()
b. Compiler issues compile time errors about it
c. Program will be executed without any interrupt
d. Program will be terminated abnormally
d. Program will be terminated abnormally
Read More21 :: Functions called from within a try block may also throw exception.
a. True
b. False
a. True
Read More22 :: Irrespective of exception occurrence, catch handler will always get executed.
a. True
b. False
b. False
Read More25 :: How can we restrict a function to throw certain exceptions?
a. Defining multiple try and catch block inside a function
b. Defining generic function within try block
c. Defining function with throw clause
d. It is not possible in CPP to restrict a function
c. Defining function with throw clause
Read More