Catching Base and Derived Classes as Exceptions in C++

Introduction

Exception is defined as an unwanted error during compilation that a program throw. Exception handling in C++ is a mechanism for dealing with errors or unexpected conditions that may occur during the execution of a program. It allows you to separate the error-handling code from the normal code and handle errors in a structured and organized way. On this page we will discuss about catching base and derived classes as exceptions in C++ .
4-NF form in DBMS

Catching Base and Derived Classes as Exceptions

The basic structure of exception handling in C++ involves using the “try” block to enclose the code that may throw an exception. The “catch” block is used to handle the exception that was thrown. The catch block is associated with the try block by following the try block immediately.

Syntax

try {
    // code that may throw an exception
} catch (exceptionType e) {
    // code to handle the exception
}

When an exception is thrown inside the try block, the program will immediately exit the current function and search for the nearest enclosing try block. If a matching catch block is found, the program will transfer control to that catch block and execute the code inside it. If no matching catch block is found, the program will terminate.

Here, we will examine the concept of exception handling and the ways to catch base and derived classes in C++.

  • If the base and derived classes that we have defined are caught as exceptions,then the catch block for the derived class will be displayed before the base class in the output terminal.
  • The above statement is correct, which can be verified by reversing the order of the base and derived classes, and in that case the catch block for the derived class will not be reached.

Example 1:

Run
#include <iostream>
using namespace std;

class Base {};
class Derived : public Base {};

int main()
{
  try
  {
    throw Derived();
  }
  catch (Base& b)
  {
    cout << "Caught base exception" << endl;
  }
  catch (Derived& d)
  {
    cout << "Caught derived exception" << endl;
  }
  
  return 0;
}

Output:

Caught base exception
Thus it is proved that if catch block of the base class is written before the catch block for the derived class then the derived class catch block will never be reached.

Example 2:

Run
#include <iostream>
using namespace std;

class Base {};
class Derived : public Base {};

int main()
{
  try
  {
    throw Derived();
  }
  catch (Derived& d)
  {
    cout << "Caught derived exception" << endl;
  }
  catch (Base& b)
  {
    cout << "Caught base exception" << endl;
  }
  return 0;
}

Output:

Caught derived exception

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription