• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception and Overriding

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule about Exception is that it has be be handled or declared and the overriding method can't throw any checked exception that are not declared for the overridden method. But why am I being forced to declare and handle the exception in the below code.
//Removing throws clause

class Overriding{
protected void test(String s){
System.out.println("In the Superclass Method test " + s);
}

public static void main(String arg[]){
Overriding superOver = new Overriding();
Override subOver = new Override();

try{
superOver.test("Hello super");
System.out.println();
System.out.println();
subOver.test("Hello sub");
}catch(IllegalAccessException e){
System.out.println(e);
}finally{
System.out.println("This is always execute");
}
}
}
class Override extends Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In method test of subclass " + s );
}
}
Removing the throws clause gives the following error message -
The method void test declared in the class Override cannot override the method of the same signature declared in the class Overriding. Their throws clause are incompatible
protected void test(String s) throws IllegalAccessException
//comment the catch block add the throws clause

class Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In the Superclass Method test " + s);
}

public static void main(String arg[]){
Overriding superOver = new Overriding();
Override subOver = new Override();

try{
superOver.test("Hello super");
System.out.println();
System.out.println();
subOver.test("Hello sub");
/*}catch(IllegalAccessException e){
System.out.println(e);*/
}finally{
System.out.println("This is always execute");
}
}
}
class Override extends Overriding{
protected void test(String s)throws IllegalAccessException{
System.out.println("In method test of subclass " + s );
}
}
Commenting the catch block gives the following error
Exception java.lang.IllegalaccessException must be caught or it must be declared in the throws clause of this method
superOver.test("Hellow super");

When both throws clause and catch block is given the programme give not error message.
In case of NullPointerException(uncheckedexception) we need to either handle or declare the exception. Why is that so?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I understand it you have answered your own question in the very first sentence. You are trying to throw an exception in a sub class that is not declared in the super class. Java won't let you do that as the compiler is telling you.
Manfred.
reply
    Bookmark Topic Watch Topic
  • New Topic