• 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

regarding exception and the overriding

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
public void test(){System.out.print("test");}}
class B extends A{
public void test() throws RuntimeException{
if(true) throw new RuntimeException();
}

The above two classes compile fine. But when I replace RuntimeException with IOException , it throws a compilation error saying that overridden method test does not throw IOException. Then how does it work with the Runtime Exception?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

that's a difference between unchecked exceptions like RuntimeExceptions (or subtypes of it) and checked exceptions like IOException!

If a parent class declares to throw a checked exception, subclasses basically have to throw at least the same checked exceptions to fulfill the contract of the parent class.

Marco
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:If a parent class declares to throw a checked exception, subclasses basically have to throw at least the same checked exceptions to fulfill the contract of the parent class.



The other way round. A child class method does not have to be declared to throw any exception, but it can't be declared to throw a checked exception that the parent class method is not declared to throw.

gives the error
ExceptionMethod() in test.ExceptionChild2 cannot override ExceptionMethod() in test.ExceptionParent; overridden method does not throw org.xml.sax.SAXException at line 23, column 0
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh sorry, I didn't look carefully enough at the source code. I just thought the inheritance in the example was the other way around.

Thanks for clarification Joanne!


Marco
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that the right way round? Surely a subclass (overridden) method is a refinement of the superclass method. So in every condition under which the superclass method executes, the subclass method must execute too. So a subclass (overridden) method must not declare any more exceptions than the superclass method.


Of course, if you declare an unchecked exception, the compiler overlooks the declaration.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was that a question to me or Marco ?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marco. Sorry, I opened the thread and posted a reply and didn't realise you were posting simultaneously.
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem! But I really was confused for a moment
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Marco Ehrentreich wrote:No problem! But I really was confused for a moment



Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer

Both.

And it gives you something to wind me up about. What I do is open several threads, read one, close it, read the second, close it, so you might have ½ hour to post before I even notice there is a reply.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Joanne Neal wrote:Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer

Both.

And it gives you something to wind me up about. What I do is open several threads, read one, close it, read the second, close it, so you might have ½ hour to post before I even notice there is a reply.



You should know that it's only we women who can multitask.
You just stick to doing one thing at a time and your life will be so much easier
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:You should know that it's only we women who can multitask.
You just stick to doing one thing at a time and your life will be so much easier

But that will spoil things for you
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic