Catching Base and Derived Classes as Exceptions in C++ and JavaBefore 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.
Algorithm (Catching Base and Derived Classes as Exceptions in C++ and Java)Example: C++ Code 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 Output: /tmp /vL tk MpH Gzo.o the code has been caught in the derived exception Java Code Output: /tmp /vL tk MpH Gzo.o ./Main.java:13: error: exception Derived has already been caught catch (Derived d) { ^ 1 error Java Code 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");} |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India