Skip to content
Related Articles

Related Articles

Catching Base and Derived Classes as Exceptions in C++ and Java

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 20 May, 2022

An Exception is an unwanted error or hurdle that a program throws while compiling. There are various methods to handle an exception which is termed exceptional handling

Let’s discuss what is Exception Handling and how we catch base and derived classes as an exception in C++:

  • If both base and derived classes are caught as exceptions, then the catch block of the derived class must appear before the base class. 
  • 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 
     

C++




// C++ Program to demonstrate a
// Catching Base Exception
#include <iostream>
using namespace std;
 
class Base {
};
class Derived : public Base {
};
int main()
{
    Derived d;
    // Some other functionalities
    try {
        // Monitored code
        throw d;
    }
    catch (Base b) {
        cout << "Caught Base Exception";
    }
    catch (Derived d) {
        // This 'catch' block is NEVER executed
        cout << "Caught Derived Exception";
    }
    getchar();
    return 0;
}


The output of the above C++ code:

prog.cpp: In function ‘int main()’:
prog.cpp:20:5: warning: exception of type ‘Derived’ will be caught
    catch (Derived d) {
    ^
prog.cpp:17:5: warning:    by earlier handler for ‘Base’
    catch (Base b) {

In the above C++ code, if we change the order of catch statements then both catch statements become reachable. 

Following is the modified program and it prints “Caught Derived Exception”

C++




// C++ Program to demonstrate a catching of
// Derived exception and printing it successfully
#include <iostream>
using namespace std;
 
class Base {};
class Derived : public Base {};
int main()
{
    Derived d;
    // Some other functionalities
    try {
        // Monitored code
        throw d;
    }
    catch (Derived d) {
        cout << "Caught Derived Exception";
    }
    catch (Base b) {
        cout << "Caught Base Exception";
    }
    getchar(); // To read the next character
    return 0;
}


Output:

Caught Derived Exception

In Java, catching a base class exception before derived is not allowed by the compiler itself. In C++, the compiler might give a warning about it but compiles the code. 

For example, the following Java code fails in compilation with the error message “exception Derived has already been caught” 
 

Java




// Java Program to demonstrate
// the error filename Main.java
class Base extends Exception {
}
class Derived extends Base {
}
public class Main {
    public static void main(String args[])
    {
        try {
            throw new Derived();
        }
        catch (Base b) {
        }
        catch (Derived d) {
        }
    }
}


Error:

prog.java:11: error: exception Derived has already been caught
   catch(Derived d) {}


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!