Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling in Over-ridden method

 
Souvik Goswami
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[Edit - added code tags - see UseCodeTags]
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to The Ranch!

What error were you expecting? Remember that ArithmeticException is a subclass of RuntimeException.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15286
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please check your private messages.

About your question: Do you mean: why don't I get a compiler error because I didn't specify ArithmeticException in the "throws" clause of the method? That is because ArithmeticException is an unchecked exception. See the page The Catch or Specify Requirement from Oracle's Java Tutorials, which explains this in more detail.
 
Souvik Goswami
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I understood that Unchecked Exceptions do not need to be thrown.
Now, is it possible to throw any Checked Exception in an over-ridden method, that is not mentioned to be thrown in the source method???
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try it and see for yourself?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Souvik Goswami wrote:Thanks, I understood that Unchecked Exceptions do not need to be thrown.


What you mean is that they don't need to be declared or caught. No exception ever needs to be thrown.



Now, is it possible to throw any Checked Exception in an over-ridden method, that is not mentioned to be thrown in the source method???


The overriding method can only throw the checked exceptions that its parent declares, and their descendants. So if the parent declares IOException, we can throw IOException, FileNotFoundException, etc., but we cannot throw SQLException.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not checked exceptions, no (unless they're a subclass of something that is declared in the parent).

The reason is that it would violate the Liskov Substitution Principle. Consider this example:
If this was allowed, the compiler would allow the try/catch block, because it would treat the variable as a SuperClass object. It knows the only checked exceptions that can throw are IOExceptions, and these are handled. But obj is actually a SubClass object, and so line 12 might actually throw a SQLException. This would mean the compiler has allowed the possibility of a checked exception not being handled - which defeats the whole point of them. The sensible way to prevent this is to not allow the method to be overridden like this.
 
Souvik Goswami
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every one.......
Rob Spoor:I was trying it out but faced some difficulties with constructor call like new XYZException(). But anyways, problem solved. Thank You all again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic