• 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

Exceptions and overriding

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi javers.
I have found that question:

and the answer is "method1 in class NewBase does not need to specify any exceptions", but as I know the overriding method must not thow new or broader checked exception that were declared by the overriden method, but at least it have to declare (or throw) the same exception or it's subclass?
So in the above code we have to declare at least IOException or it's subclass, haven't it?
Thanks in advance.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check out the JLS, §8.4.4 Method Throws, you'll see this:


A method that overrides or hides another method (�8.4.6), including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.
More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration n in B overrides or hides a method declaration m in A. If n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its superclasses must occur in the throws clause of m; otherwise, a compile-time error occurs.


To break that down, an overriding method can throw any subset of checked exceptions declared by the overridden method. That subset can include the entire set (meaning that the overriding method can throw all of the exceptions the overridden method can throw) and it can also include the empty set (meaning that the overriding method throws no expceptions whatsoever).
The key is that the overriding method can not throw a checked exception that the overridden method does not declare.
I hope that helps,
Corey
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets look at a reason behind this design:

In case 1, the compiler knows that method1 might throw IOException
In case 2, at runtime method1 (the overriding version in NewBase) doesn't throw IOException, however at compile time the decision has to be made based on the reference type, ie Base's method1 signature.
So, if the runtime class is a subclass of the parent, and the subclass's overriding method was allowed to throw newer or broader exceptions then common exception handling based on the parent reference's method signature would not be possible. Case 3 illustrates this scenario.
The crucial point is that using a base class reference and providing the runtime via any subclass is a powerful paradigm and would break if this exception handling rule wasn't enforced.
- ortimuS
[ April 06, 2004: Message edited by: ortimus tilap ]
 
Let's get him boys! We'll make him read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic