• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overriding : Throwing Exceptions  RSS feed

 
samarth mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this Question while preparing for OCAJP

Q.

How can you fix the following code to make it compile:


Assume that changes suggested in a option are to be applied independent of other options.

Options:
A>Change doStuff in Amazing to throw only IllegalArgumentException.
B>Change doStuff in Great to throw only FileNotFoundException as well as IllegalArgumentException.
C>Change doStuff in Amazing to throw only IOException.
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException.
E>Replace g.doStuff() to ((Amazing) g).doStuff().

Corrent Answers are A and D. But, why can't it be C and D?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SCJP 6 book by Kathe Sierra explains it nicely as:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.


In your example C is not valid as IOException is a super class of FileNotFoundException.
 
samarth mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:The SCJP 6 book by Kathe Sierra explains it nicely as:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.


In your example C is not valid as IOException is a super class of FileNotFoundException.


I would like you to read the options once again.
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samarth mishra wrote:
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.

Yes but option C doesn't meet that criteria.

The super class would throw FileNotFoundException and the sub class would throw IOException
 
samarth mishra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
samarth mishra wrote:
I have read the concepts. But, if both the method(Overriding and Overridden) throw the same exceptions, then it's fine.

Yes but option C doesn't meet that criteria.

The super class would throw FileNotFoundException and the sub class would throw IOException


These are the 2 options which say that for super and sub class only IOException will be thrown.

C>Change doStuff in Amazing to throw only IOException. {Amazing Class wchich is the Subclass throws IOException}
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException. {Great Class which is the Superclass throws IOException}
So, I am asking what is wrong with it.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samarth mishra wrote:These are the 2 options which say that for super and sub class only IOException will be thrown.

C>Change doStuff in Amazing to throw only IOException. {Amazing Class wchich is the Subclass throws IOException}
D>Change doStuff in Great to throw only IOException instead of FileNotFoundException. {Great Class which is the Superclass throws IOException}
So, I am asking what is wrong with it.

Oh I think I see what you mean which is if you do both C & D the code is valid.
Read the question again, you haven't understood it.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I interpreted option C as: Change the Subclass "Amazing" so that it no longer throws IllegalArgumentException.

That would be correct if IllegalArgumentException was a checked exception, because the superclass does not declare it.
However because IllegalArgumentException is a runtime exception, it isn't actually causing any problem.
Making this change wouldn't remove the compile error in the code as given.

Parent class throws FileNotFoundException
Child class throws IOException --> error because IOException is broader than FileNotFoundException

 
A. Costa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone explain me why B is not correct as well? "Change doStuff in Great to throw FileNotFoundException and IllegalArgumentException." That would fix the overriding problem with doStuff method in Amazing class and it wouldn't get a compile error in main method because it throws an IOException wich is a parent of FileNotFoundException. What am I not getting right here?

Sorry for reopening a thread.

Cheers.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have it backwards. A method in a subclass cannot throw a more general checked exception than any of its superclasses. IOException is more general than FileNotFoundException. Therefore, Amazing.doStuff cannot legally declare that it throws IOException because Great.doStuff only throws FileNotFoundException.

This has to do with the Liskov Substitution Principle. Allowing Amazing.doStuff to throw IOException when it's superclass can only throw FileNotFoundException violates LSP.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So does throwing an IllegalArgumentException. If the superclass method accepts all arguments, then the subclass method must accept all arguments. The difference is that the IllegalArgumentException is an unchecked exception and the javac tool does not notice it. Just because the code compiles does not imply there is anything correct about it.
 
A. Costa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I just misread that. Thanks a million.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!