• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Need Explanation..

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why this code not shows error and ..throws Exception... will also applicable for child class...

class Parent {
void say() throws Exception {
}
}

class Child extends Parent {
void say() {
}
}
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When overriding a method, you can throw either restrictive type exception, or can not throw any exception at all.

And in overloading, no rules apply in regard to throwing an exception. You can throw any type of exception or not throw any exception at all.
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that mean, if the overloaded method throws an exception.then the other method also throws the exception?
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please anyone clear more this concept?
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overriding method need not declare throws clause eventhough the base class method(which is being overridden) has some exceptions(checked or unchecked) in its throw clause.

A method can declare to throw Exception(not any checked exception) or RuntimeException eventhough its method body doesn't throw any exception
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overridden method cannot throw newer or broader exception. It can throw no exception or any unchecked exception.

If the super class method doesnt throw any exception, then the overriden method cannot throw any exception.

In case of overloaded methods, none of these rules are applicable.
[ October 07, 2008: Message edited by: chander shivdasani ]
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a little bit more care in how that is worded:

When overriding you can not throw new or wider(superclass of the overridden checked exception) CHECKED exceptions.
You MAY throw narrower (subclass of overridden) CHECKED or any UNCHECKED exception.

I think that the reasoning comes from this: when you are overriding a method you should KEEP the API the same so that you don't break calling methods. What I mean is, if another method calls into a method that you decide to override later....if wider or new checked exceptions were allowed you would break the originall calling code because it would have been set to handle the exceptions that existed originally. If you throw new or wider all of a sudden the calling method may not have a catch clause for the new/broader exception.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic