Open In App

Top C++ Exception Handling Interview Questions and Answers

Improve
Improve
Like Article
Like
Save
Share
Report

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.

C++ Exception Handling Interview Questions

In this article, we will learn about the top 15 most frequently and most asked interview questions on Exception Handling.

Exception Handling Interview Questions and Answers

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 be originated 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.

3. What is Exception Handling?

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:

  1. Separate Error code from Normal code to help us understand errors easily.
  2. Functions/Methods can be handled only by the exceptions they choose. The exceptions not chosen will be handled by the caller.
  3. Exceptional handling allows the grouping of error types which helps in categorizing them.
  4. It makes the program’s error type easy to understand.

5. How to implement exception handling in C++?

C++ supports exception handling. It is implemented by try{ } and catch( ){ } statements.

  • The try statement allows you to define a block of code to be tested for errors while it is being executed.
  • The throw keyword throws an exception when a problem is detected, which lets us create a custom error.
  • The catch statement allows you to define a block of code to be executed if an error occurs in the try block.

It follows certain rules:

  • The catch can be executed in multiple ways according to the argument thrown by the try.
  • There can be multiple catch( ) but only a single try.
  • If try throws but catch is not able to catch it then terminate() will be called by default.
  • If no argument is executed it will just continue to the statement after the catch block.

Example:

try{
    throw 10;  //catch(int x)
    throw "x"; //catch(...)
}
catch(int x){
    //int catch
}
catch(...){
    //default catch
}

6.  What is the use of terminate( ) in C++?

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;
}


Output

First throw called
Rethrowing throw called

10. 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;
}


Output

Default Exception

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.

12. Write the output of the following code with the explanation.

C++




#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;
}


Output

Before Error
Exception Caught 
After catch 

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.

13. Write the output of the following code with the explanation.

C++




#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

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“ 

14. Write the output of the following code with the explanation.

C++




#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

Explanation: The default catch block should be the last catch block or else other catch blocks will never be reached.

15. Write the output of the following code with the explanation.

C++




#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;
}


Output

Inner Catch
Outer Catch

Explanation: “Inner Catch” is called followed by “Outer Catch” because of rethrowing exceptions.

Bonus Questions:

1. What is an error in C++?

  • Violation of syntactic and semantic rules of a language
  • Missing Semicolons
  • Missing double quotes
  • Violation of program interface

Answer: Violation of syntactic and semantic rules of a languages

2. What program does by default when detecting an exception?

  • Continue running
  • Termination of the program
  • Calls other functions of the program
  • Removes the exception and tells the programmer about an exception

Answer: Termination of the program

3. Which of the following is an exception in C++?

  • A number divided by zero
  • Semicolon not written
  • Variable not declared
  • Wrong written expression

Answer: A Number divided by zero

4. Throwing an unhandled exception causes standard library function _______________ to be invoked.

  • stop()
  • aborted()
  • terminate()
  • abandon()

Answer: terminate()

5. Catch handler can have multiple parameters.

  • True
  • False

Answer: False

6. What will be the output of this code? Choose the correct option

C++




#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;
}


  • This is exception
  • Syntax error
  • No output
  • The program crashed at runtime 

Answer: The program crashed at runtime

7.  What will be the output of this code? Choose the correct option

C++




#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* e) {
        cout << "This is exception" << e << endl;
    }
    return 0;
}


  • This is an exception
  • Syntax error
  • No output
  • The program crashed at runtime

Answer: This is an exception



Last Updated : 10 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads