Handling Exceptions in Polymorphic Calls

This program demonstrates handling exceptions in polymorphic calls.

Problem Statement

Write a Java program where polymorphic calls throw exceptions handled by caller.

Source Code

1 class Base {
2 void risky() throws Exception {
3 throw new Exception("Eduinq Base Exception");
4 }
5 }
6 class Derived extends Base {
7 void risky() throws Exception {
8 throw new Exception("Eduinq Derived Exception");
9 }
10 }
11 public class PolymorphismExceptionExample {
12 public static void main(String[] args) {
13 System.out.println("Eduinq Polymorphism Exception Example");
14 Base b=new Derived();
15 try {
16 b.risky();
17 } catch(Exception e) {
18 System.out.println("Eduinq Exception: "+e.getMessage());
19 }
20 }
21 }

Program Output

					Eduinq Polymorphism Exception Example
Eduinq Exception: Eduinq Derived Exception
				

Explanation

Polymorphic calls may throw exceptions from subclass implementations. - Caller must handle exceptions generically. - Improves flexibility and error management. - In enterprise systems, polymorphism exception handling ensures extensibility. Conclusion: Shows handling exceptions in polymorphic calls.

Quick Links to Explore