Last Updated : 06 Nov, 2024
Exception Handling is one of the most important topics from the C++ Interview perspective. Exception handling is an effective means to handle the runtime errors that disrupt the normal flow of the program. It is one of the most used concepts in real-world embedded systems so it is common to encounter questions based on exception handling during interviews.
In this interview preparation article, we will learn about the top 15 most frequently and most asked interview questions on Exception Handling.
1. What is an Exception?Conditions responsible for creating errors during the execution of a program are known as Exceptions. These errors can interrupt the execution of the program and if the program can’t handle these exceptions then OS handles them and the program is terminated abruptly.
2. Explain Synchronous Exceptions and Asynchronous Exceptions.Synchronous exceptions are the exceptions that occur at a particular instruction. They can only originate from throw expressions and are caused due to errors like incorrect input or array out-of-index access in a program.
Asynchronous exceptions are the exceptions that create errors that are not controllable by the program. For example-hardware malfunctions, disk failure, etc.
Conditions responsible for creating errors during the execution of a program are known as Exceptions. Handling these exceptions by either removing these conditions or by using some other operations than normal operations is known as exception handling. Exception handling is an effective means to handle the runtime errors that disrupt the normal flow of the program.
4. Why do we need exception Handling?We use Exception Handling for the following reasons:
C++ supports exception handling. It is implemented by try{ } and catch( ){ } statements.
It follows certain rules:
Example:
try{6. What is the use of terminate( ) in C++?
throw 10; //catch(int x)
throw "x"; //catch(...)
}
catch(int x){
//int catch
}
catch(...){
//default catch
}
The terminate( ) function is used to abort the program by default rather than throwing it for a catch. The terminate( ) function can be used for the exceptions which can't be handled instead we just abort them. The terminate() functions calls the terminate_handler which by default calls the abort() function. For example:
if(x==0){
terminate();
}
In this program, if x==0, it will automatically end the file with a note saying "terminate called without an active exception".
7. What is the use of unexpected( ) in C++?The unexpected() function is called when the exception thrown by a function is a type not listed in the exception specification for the function. The unexpected( ) will call the unexpected_handler that by default calls the terminate().
8. What will happen if an exception is thrown but not caught anywhere?When an exception is thrown but not caught anywhere, the program will terminate abnormally.
9. Explain the concept of Rethrowing exceptions?A rethrowing exception is a term used when we throw the exception again from one catch block to another. The exception is thrown towards another outside catch block.
Example:
C++
#include <iostream>
using namespace std;
void help()
{
try {
throw 10;
}
catch (...) {
cout << "First throw called\n";
throw;
}
}
int main()
{
try {
help();
}
catch (...) {
cout << "Rethrowing throw called\n";
}
return 0;
}
First throw called Rethrowing throw called10. What is the difference between exception handling in C++ and Java?
The following table list the differences between exception handling in C++ and Java:
Java
C++
Only throwable objects can be thrown as exceptions. All types can be thrown as exceptions e.g. int, char. A special block called finally is always executed after the try-catch block. There is no such block in C++. We can catch Exception objects to catch all kinds of exceptions. Because normally we do not catch Throwable(s) other than Exception(s). There is a special catch called “catch all” that can catch all kinds of exceptions. A special keyword throws is used to list exceptions that can be thrown by a function. The keyword throw is used to list exceptions that can be thrown by a function. There are two types of exceptions in Java – checked and unchecked. All exceptions in C++ are unchecked. Handling the exception in Java is relatively easier. Handling the exception in C++ is more difficult. 11. Write the output of the following code with the explanation. C++
#include <iostream>
using namespace std;
int main()
{
try {
throw 10;
}
catch (char* excp) {
cout << "Caught " << excp;
}
catch (...) {
cout << "Default Exception\n";
}
return 0;
}
12. Write the output of the following code with the explanation. C++Explanation: An integer value 10 is thrown as an exception. It can't be interpreted as char * means we need to go with default catch that is why the "Default Exception" is printed.
#include <iostream>
using namespace std;
int main()
{
int x = -10;
try {
cout << "Before Error\n";
if (x < 0) {
throw x;
cout << "After Error\n";
}
}
catch (int x) {
cout << "Exception Caught \n";
}
cout << "After catch \n";
return 0;
}
Before Error Exception Caught After catch
13. Write the output of the following code with the explanation. C++Explanation: When a throw statement is encountered, the program control skips all the remaining statements of that block and goes directly to the corresponding catch block. That is why "After Error" is not printed.
#include<iostream>
using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Derived d;
try {
throw d;
}
catch(Base b) {
cout<<"Caught Base Exception";
}
catch(Derived d) {
cout<<"Caught Derived Exception";
}
return 0;
}
Output:
Caught Base Exception
14. Write the output of the following code with the explanation. C++Explanation: If both base and derived classes are caught as exceptions, then the catch block of the derived class must appear before the base class because if we put the base class first then the derived class catch block will never be reached. For example, the following C++ code prints “Caught Base Exception“
#include <iostream>
using namespace std;
int main()
{
try {
throw 10;
}
catch (...) {
cout << "default exception \n";
}
catch (int param) {
cout << "integer exception \n";
}
return 0;
}
Output:
An error will be raised
15. Write the output of the following code with the explanation. C++Explanation: The default catch block should be the last catch block or else other catch blocks will never be reached.
#include <iostream>
using namespace std;
int main()
{
try {
try {
throw 10;
}
catch (int n) {
cout << "Inner Catch\n";
throw;
}
}
catch (int x) {
cout << "Outer Catch\n";
}
return 0;
}
Inner Catch Outer Catch
C++ Exception Handling Interview Questions Bonus Questions: 1. What is an error in C++?Explanation: "Inner Catch" is called followed by "Outer Catch" because of rethrowing exceptions.
2. What program does by default when detecting an exception?Answer: Violation of syntactic and semantic rules of a languages
3. Which of the following is an exception in C++?Answer: Termination of the program
4. Throwing an unhandled exception causes standard library function _______________ to be invoked.Answer: A Number divided by zero
5. Catch handler can have multiple parameters.Answer: terminate()
6. What will be the output of this code? Choose the correct option C++Answer: False
#include <iostream>
using namespace std;
int main()
{
try {
int x = 1;
int y = 0;
int ans;
ans = x / y;
}
catch (exception E) {
cout << "This is exception" << endl;
}
return 0;
}
7. What will be the output of this code? Choose the correct option C++Answer: The program crashed at runtime
#include <iostream>
using namespace std;
int main()
{
try {
int x = 1;
int y = 0;
int ans;
if (y == 0)
throw "divide by zero";
ans = x / y;
}
catch (char const* e) {
cout << "This is exception " << e << endl;
}
return 0;
}
// This code is modified by Susobhan Akhuli
Answer: This is an exception
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4