C++ Exception Handling

Question 1
#include <iostream>
using namespace std;
int main()
{
   int x = -1;
   try {
      cout << "Inside try \n";
      if (x < 0)
      {
         throw x;
         cout << "After throw \n";
      }
   }
   catch (int x ) {
      cout << "Exception Caught \n";
   }

   cout << "After catch \n";
   return 0;
}
Cross
Inside try
Exception Caught
After throw 
After catch
Tick
Inside try
Exception Caught
After catch
Cross
Inside try
Exception Caught
Cross
Inside try
After throw
After catch


Question 1-Explanation: 
When an exception is thrown, lines of try block after the throw statement are not executed. When exception is caught, the code after catch block is executed. Catch blocks are generally written at the end through.
Question 2
What is the advantage of exception handling ?
  1. Remove error-handling code from the software's main line of code.
  2. A method writer can choose to handle certain exceptions and delegate others to the caller.
  3. An exception that occurs in a function can be handled anywhere in the function call stack.
Cross
Only 1
Tick
1, 2 and 3
Cross
1 and 3
Cross
1 and 2


Question 2-Explanation: 
Advantage of exception handling are :-
  1. Remove error-handling code from the software’s main line of code.
  2. A method writer can choose to handle certain exceptions and delegate others to the caller.
  3. An exception that occurs in a function can be handled anywhere in the function call stack.
  4. Separating Error-Handling Code from “Regular” Code.
  5. Propagating Errors Up the Call Stack.
Question 3

What should be put in a try block?

1. Statements that might cause exceptions
2. Statements that should be skipped in case of an exception 
Cross

Only 1

Cross

Only 2

Tick

Both 1 and 2



Question 3-Explanation: 

The statements which may cause problems are put in try block. Also, the statements which should not be executed after a problem occurred, are put in try block. Note that once an exception is caught, the control goes to the next line after the catch block.

Question 4
Output of following program
#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;
}
Cross
Caught Derived Exception
Tick
Caught Base Exception
Cross
Compiler Error


Question 4-Explanation: 
If both base and derived classes are caught as exceptions then catch block of derived class must appear before the base class. If we put base class first then the derived class catch block will never be reached. In Java, catching a base class exception before derived is not allowed by the compiler itself. In C++, compiler might give warning about it, but compiles the code.
Question 5
#include <iostream>
using namespace std;

int main()
{
    try
    {
       throw 'a';
    }
    catch (int param)
    {
        cout << "int exception\n";
    }
    catch (...)
    {
        cout << "default exception\n";
    }
    cout << "After Exception";
    return 0;
}
Tick
default exception
After Exception
Cross
int exception
After Exception
Cross
int exception
Cross
default exception


Question 5-Explanation: 
The block catch(...) is used for catch all, when a data type of a thrown exception doesn\'t match with any other catch block, the code inside catch(...) is executed. Note that the implicit type conversion doesn\'t happen when exceptions are caught. The character \'a\' is not automatically converted to int.
Question 6
#include <iostream>
using namespace std;

int main()
{
    try
    {
       throw 10;
    }
    catch (...)
    {
        cout << "default exception\n";
    }
    catch (int param)
    {
        cout << "int exception\n";
    }

    return 0;
}
Cross
default exception
Cross
int exception
Tick
Compiler Error


Question 6-Explanation: 
It is compiler error to put catch all block before any other catch. The catch(...) must be the last catch block.
Question 7
#include <iostream>
using namespace std;

int main()
{
    try
    {
        try
        {
            throw 20;
        }
        catch (int n)
        {
            cout << "Inner Catch\n";
            throw;
        }
    }
    catch (int x)
    {
        cout << "Outer Catch\n";
    }
    return 0;
}
Cross
Outer Catch
Cross
Inner Catch
Tick
Inner Catch
Outer Catch
Cross
Compiler Error


Question 7-Explanation: 
The statement \'throw;\' is used to re-throw an exception. This is useful when a function can handles some part of the exception handling and then delegates the remaining part to the caller. A catch block cleans up resources of its function, and then rethrows the exception for handling elsewhere.
Question 8
#include <iostream>
using namespace std;

class Test {
public:
   Test() { cout << "Constructing an object of Test " << endl; }
  ~Test() { cout << "Destructing an object of Test "  << endl; }
};

int main() {
  try {
    Test t1;
    throw 10;
  } catch(int i) {
    cout << "Caught " << i << endl;
  }
}
Cross
Caught 10
Cross
Constructing an object of Test 
Caught 10
Tick
Constructing an object of Test 
Destructing an object of Test 
Caught 10
Cross
Compiler Errror


Question 8-Explanation: 
When an object is created inside a try block, destructor for the object is called before control is transferred to catch block.
Question 9
#include <iostream>
using namespace std;

class Test {
  static int count;
  int id;
public:
  Test() {
    count++;
    id = count;
    cout << "Constructing object number " << id << endl;
    if(id == 4)
       throw 4;
  }
  ~Test() { cout << "Destructing object number " << id << endl; }
};

int Test::count = 0;

int main() {
  try {
    Test array[5];
  } catch(int i) {
    cout << "Caught " << i << endl;
  }
}
Cross
Constructing object number 1
Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 1
Destructing object number 2
Destructing object number 3
Destructing object number 4
Caught 4
Tick
Constructing object number 1
Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 3
Destructing object number 2
Destructing object number 1
Caught 4
Cross
Constructing object number 1
Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 4
Destructing object number 3
Destructing object number 2
Destructing object number 1
Caught 4
Cross
Constructing object number 1
Constructing object number 2
Constructing object number 3
Constructing object number 4
Destructing object number 1
Destructing object number 2
Destructing object number 3
Caught 4


Question 9-Explanation: 
The destructors are called in reverse order of constructors. Also, after the try block, the destructors are called only for completely constructed objects.
Question 10
Which of the following is true about exception handling in C++? 1) There is a standard exception class like Exception class in Java. 2) All exceptions are unchecked in C++, i.e., compiler doesn't check if the exceptions are caught or not. 3) In C++, a function can specify the list of exceptions that it can throw using comma separated list like following.
  void fun(int a, char b) throw (Exception1, Exception2, ..) 
Cross
1 and 3
Tick
1, 2 and 3
Cross
1 and 2
Cross
2 and 3


There are 12 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads