Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In exception handling, it is known that, if the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception. Why so? consider the following example :

import java.io.*;  
class Parent{  
  void msg(){System.out.println("parent");}  
}  
  
class TestExceptionChild extends Parent{  
  void msg()throws IOException{  
    System.out.println("TestExceptionChild");  
  }  
  public static void main(String args[]){  
   Parent p=new TestExceptionChild();  
   p.msg();  
  }  
}


What I have tried:

We get compilation error here. If I need to read a file in the overridden method "msg", then I have to mention "throws IOException" there. But java doesn't allow them. Can anyone explain this?
Posted
Updated 27-May-18 4:50am
v2
Comments
Richard MacCutchan 27-May-18 11:16am    
You are trying to change the signature of the msg method which is not allowed. You need to catch the exception in the block the calls the child method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900