• 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

Exception Handling in Over-ridden method

 
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[Edit - added code tags - see UseCodeTags]
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to The Ranch!

What error were you expecting? Remember that ArithmeticException is a subclass of RuntimeException.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it and see for yourself?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic