• 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

overriding exceptions

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overriding method must not throw any exceptions not thrown by the overridden method.
I have seen this as true and false on mock exams. The ones that claim it as true point out that runtime exceptions can be thrown. The restriction is only for CHECKED exceptions. Which is correct for the exam?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 8.4.4


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.


I think JLS makes it clear, tell us if not !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
[This message has been edited by Valentin Crettaz (edited November 24, 2001).]
 
Jeff Gaer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JLS is clear it is the mock exam questions that bug me. Notice the question does not specify checked or un checked. On one exam that made the statment false since only 'Checked' exceptions matter, the other exam seemed to assume checked was implied ( since why would any one write a throws statement for unchecked exceptions? ).
Which is the correct interpretation for the exam?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry the wording in the exam will be clear. Strictly speaking the answer to your first question would be false since Exception gathers checked AND unchecked exceptions together and there is no need to declare unchecked exceptions. Moreover, unchecked exceptions may be thrown from overriding method as you already know, although they are not thrown from the overridden method.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic