«Back to Home

Core Java

Topics

ExceptionHandling With MethodOverriding In Java

Exception handling with Methodoverriding

Key points of exception handling with Methodoverriding are,
  • In Java, if the parent class method does not declare an exception, the child class overridden method cannot declare the checked exception but child class overridden method can declare an unchecked exception.

  • In Java, if the parent class method declares an exception, the child class overridden method can declare same, child class exception or no exception but cannot declare a parent class exception.
When the parent class method does not declare an exception
 
Let’s see an example, given below.
 
Code
  1. package exceptionHandling;  
  2. import java.io.*;  
  3. class Dog {  
  4.     void eat() {  
  5.         System.out.println("Dog can eat all types of food");  
  6.     }  
  7. }  
  8. public class Puppy extends Dog {  
  9.     void eat() throws IOException { // compile time error  
  10.         System.out.println("Puppy can eat light food only");  
  11.     }  
  12.     public static void main(String args[]) {  
  13.         Dog p = new Puppy();  
  14.         p.eat();  
  15.     }  
  16. }  
39
 
In the example, shown above, Dog (parent) class method does not declare an exception but Puppy (child) class overridden method cannot declare the checked exception in Java.
 
Let’s see another example, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Dog {  
  6.   
  7.     void eat() {  
  8.         System.out.println("Dog can eat all types of food");  
  9.     }  
  10. }  
  11.   
  12. public class Puppy extends Dog {  
  13.   
  14.     void eat() throws ArithmeticException {  
  15.         System.out.println("Puppy can eat light food only");  
  16.     }  
  17.   
  18.     public static void main(String args[]) {  
  19.         Dog p = new Puppy();  
  20.         p.eat();  
  21.     }  
  22. }  
40

Output

41
 
In the above example, Dog (parent) class method does not declare an exception but Puppy (child) class overridden method can declare unchecked exception in Java.
 
When the parent class method declares an exception
 
Let’s see an example, given below.
 
Code
  1. package exceptionHandling;  
  2. import java.io.*;  
  3. class Dog {  
  4.     void eat() throws ArithmeticException {  
  5.         System.out.println("Dog can eat all types of food");  
  6.     }  
  7. }  
  8. public class Puppy extends Dog {  
  9.     void eat() throws Exception { // compile time error  
  10.         System.out.println("Puppy can eat light food only");  
  11.     }  
  12.     public static void main(String args[]) {  
  13.         Dog p = new Puppy();  
  14.         try {  
  15.             p.eat();  
  16.         } catch (Exception e) {}  
  17.     }  
  18. }  
42

In the example, shown above, Dog (parent) class method declares an exception but Puppy (child) class overridden method can declare same, child class exception or no exception but cannot declare parent class exception. Thus, it gives a compile time error because puppy class declares Dog class exception.
 
Let’s see another example, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Dog {  
  6.   
  7.     void eat() throws Exception {  
  8.         System.out.println("Dog can eat all types of food");  
  9.     }  
  10. }  
  11.   
  12. public class Puppy extends Dog {  
  13.   
  14.     void eat() throws Exception {  
  15.         System.out.println("Puppy can eat light food only");  
  16.     }  
  17.   
  18.     public static void main(String args[]) {  
  19.         Dog p = new Puppy();  
  20.         try {  
  21.             p.eat();  
  22.         } catch (Exception e) {}  
  23.     }  
  24. }  
43
Output

44

In the example, shown above, Dog (parent) class method declares an exception but Puppy (child) class overridden method can declare the same exception.
 
Let’s see another example, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Dog {  
  6.   
  7.     void eat() throws Exception {  
  8.         System.out.println("Dog can eat all types of food");  
  9.     }  
  10. }  
  11.   
  12. public class Puppy extends Dog {  
  13.   
  14.     void eat() throws ArithmeticException {  
  15.         System.out.println("Puppy can eat light food only");  
  16.     }  
  17.   
  18.     public static void main(String args[]) {  
  19.         Dog p = new Puppy();  
  20.         try {  
  21.             p.eat();  
  22.         } catch (Exception e) {}  
  23.     }  
  24. }  
45
Output
46

In the example, shown above, Dog (parent) class method declares an exception but Puppy (child) class overridden method can declare the child class exception.
 
Let’s see another example, given below.
 
Code
  1. package exceptionHandling;  
  2.   
  3. import java.io.*;  
  4.   
  5. class Dog {  
  6.   
  7.     void eat() throws Exception {  
  8.         System.out.println("Dog can eat all types of food");  
  9.     }  
  10. }  
  11.   
  12. public class Puppy extends Dog {  
  13.   
  14.     void eat() {  
  15.         System.out.println("Puppy can eat light food only");  
  16.     }  
  17.   
  18.     public static void main(String args[]) {  
  19.         Dog p = new Puppy();  
  20.         try {  
  21.             p.eat();  
  22.         } catch (Exception e) {}  
  23.     }  
  24. }  
47

Output

48

In the example, shown above, Dog (parent) class method declares an exception but Puppy (child) class overridden method declares no exception.
 
Summary
 
Thus, we learnt the usage of ExceptionHandling with MethodOverriding in Java with the examples and also learnt its rules.