Before diving straight into our topic, Catching Base and Derived Classes as Exceptions in C++ and Java, let us understand what exceptions and exception handling are. Exceptions are the errors which occur while we are programming and are generally treated as unwanted errors or to understand better. They are like the hurdles caused while programming, and exception handling is the error handling mechanism to treat the exceptions or the limitations that occur.
Various methods to handle exception handling and base and derived class catch as part of exception in both C++ and Java programming languages.
- In a scenario where the base and the derived classes we have declared are caught in the mechanism as exceptions, then we must see at the display output terminal that the catch block of the said derived class appears before the type of base we declared.
- The above statement is valid, and we can even recheck if we put our base class before the derived class, then the derived class which we have declared will never be reached.
Algorithm (Catching Base and Derived Classes as Exceptions in C++ and Java)
Example:
Example
begin //code starts from here
declare a class b.
say another class d which inherits class b.
now display an object of class d.
Try: throw derived.
Catch (d derived)
Print "code has been caught in the derived exception".
Catch (B b)
Print "code has been caught in the base exception".
End
C++ Code
Example
//here, we are writing down the C++ programming language code to
//demonstrate the concept of Catching Base and
//Derived Classes as Exceptions in both C++ and Java languages
#include <iostream>
using namespace std;
class base_ {
};
class derived_: public base_ {
};
int main()
{
derived_ d;
//these are some necessary functionalities we may use
try {
//this is the monitored code we use
throw d;
}
catch (base_ b) {
cout << "OH! Caught Based Exception occured";
}
catch (derived_ d) {
//note: the catch block we wrote below will never get executed!
cout << "oh! caught in the derived exception...";
}
getchar();
return 0;
}
Output:
Output
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 the earlier handler for 'base_.'
catch (base_ b) {
/ tm p /vLt kM pH G zo.o
OH! Caught Based Exception occurred
C++ code
Example
//here, we are writing down the C++ programming language code to
//demonstrate the concept of Catching Base and
//Derived Classes as Exceptions in both C++ and Java languages
#include <iostream>
using namespace std;
class base_ {};
class derived : public base_ {};
int main()
{
derived d;
//these are some necessary functionalities we may use
try {
//this is the monitored code we use
throw d;
}
catch (derived d) {
cout << "the code has been caught in the derived exception";
}
catch (base_ b) {
cout << "the code has been caught in the base exception";
}
//this below code snippet is to read out the next possible character
getchar();
return 0;
}
Output:
Output
/tmp /vL tk MpH Gzo.o
the code has been caught in the derived exception
Java Code
Example
//here, we are writing down the Java programming language code to
//demonstrate the concept of Catching Base and
//Derived Classes as Exceptions in both C++ and Java languages
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) {
}
}
}
Output:
Output
/tmp /vL tk MpH Gzo.o
./Main.java:13: error: exception Derived has already been caught
catch (Derived d) {
^
1 error
Java Code
Example
//here, we are writing down the Java programming language code to
//demonstrate the concept of Catching Base and
//Derived Classes as Exceptions in both C++ and Java languages
class base_b extends Exception {}
class derived_d extends base_b {}
public class Main {
public static void main(String args[]) {
// below, we wrote the try block code snippet
try {
//this is the monitored code we use
throw new derived_d();
}
catch(base_b) {
System.out.println("the code has been caught in the base exception");
}
catch(derived_d) {
System.out.println("the code has been caught in the derived exception");
}
}
}
Output:
Output
Main.java:12: error: exception Derived has already been caught
catch(derived_d) { System.out.println("the code has been caught in the derived exception");}