• 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

Checked Exceptions

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a question about checked exceptions. I have seen the folowing statement:
"When you extend a class & override a
method, the new method can not be declared as throwing checked
Exceptions other than those that were declared by the original
method."
Ex: (from RHE)
public Base {
public int Amethod () throws IOException {}
}
public Sub1 extends Base {
public int Amethod () throws IOException {} // ok
}
public Sub2 extends Base {
public int Amethod () throws IOException {} // ok
}
public Sub3 extends Base {
public int Amethod () {} // illegal
}
public Sub4 extends Base {
public int Amethod () throws Exception {} // illegal
}
public Sub5 extends Base {
public int Amethod () throws MalformedException {} // ok
}
I do not understand why Sub3 Amethod & Sub4 Amethod are illegal
& the Sub5 Amethod is ok.
Fernando
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi fernando ,
i think sub3 should be legal .
sub4 is correct and sub5 is also legal .
bcoz as per rule overriding method can throw exceptions , those r thrown by overridden method or can throw exceptions those r subclass of the exception thrown by overridden method .this is true only for checked exceptions .
so as per rule sub4 is throwing an exception which is base class exception of amethod()which is illegal and sub5 is throwing an exception which is subclass of ioexception so legal .
only doubt is about sub3
actually sub3 should work .
so please check that out again .
bye rahul
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Fernando
i think that if u declare in a super class that a method throws an exception, then u over ride that method in a subclass without declaring that it throws an exception, that is illegal.
 
lee dalais
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but then again i tried similiar code and it didn't give any problem.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When you override a method, the overriding method cannot throw any checked exception not thrown by the overriden one.
So if a method m() in class Super throws a IOException and we override m() in class Sub that extends Super, then if m() in Sub declares that throws any checked exception, they have to be IOException or a subclass of it.
So the method in Sub3 is ok

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lee I think when you override a method that throws checked exceptions you must either declare the same exception or a subclass exception of the one declared in the overridden method or you don't have to declare any exceptions in the overriding method.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic