• Post Reply Bookmark Topic Watch Topic
  • New Topic

Throwing unchecked exception from overrides method from a subclass  RSS feed

 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preparing to take OCA in June. I got some questions about exception.

I have read that overrides method from a subclass is not allowed to add new checked exception. What about uncheck exception like NullPointerException? does it allow to throw from method which overrides from a subclass? I understand that uncheck exceptions like NullPointerException should not be handled but it doesn't mean we can't handle or can handle right?

Below example is from OCA book

class CanNotHopException extends Exception{}

class Hopper{
public void hop(){}
}
public class SubClass extends Hopper{
public static void main(){
SubClass1 obj = new SubClass1();
obj.hop();
}

public void hop() throws CanNotHopException{} //If this method is not allowed to add new checked exception, what about uncheck exception like NullPointerException,is it allowed to add regardless of the fact that unchecked exceptions should not be handled.
}

Thanks
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I have tested throwing unchecked exception. It works fine. So I believe that it's ok to add new unchecked exception from a subclass.

Thanks,
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java you can throw unchecked exception from overridden method but you can't throw checked exception if method in superclass doesn't declare it.
But you can throw subclass checked exception.
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark for your reply
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spencers wrote:In java you can throw unchecked exception from overridden method . . .
That isn't quite true. It is a breach of the Liskov Substitution Principle; you are restricting feasibility of the overriding method. Throwing an unchecked exception is horrible design, and cheating. If the overridden method accepts null as an argument, then I expect the overriding method to accept null as an argument. If the overriding method throws a NullPoint‍erException, then I shall be annoyed that my method call doesn't work.I shall be very annoyed with whoever wrote the Foo class if there is an Exception in line 6. That is a breach of the principles of OO programming: a Foo isn't a Bar any more.

What you actually mean is, the compiler won't notice the unchecked Exception. That is completely different from any sort of good or correct design.
Of course certification exams aren't about good design, so for the exam you would have to say that code compiles without error. If the question says something about Exception at runtime, that would be a correct answer for the exams.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!