• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Exception and Overriding

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Skool. Stay in. Smartness. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic