Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Override Method Exception

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends..
Is there any specific rule while throwing exception in override method?(i got the error ...exception can not be thrown in override method).

Thanx in Advance!
Sachin Dimble.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you writing about a compile time error or a runtime error?

An overriding method can only throw a subset of the overridden method's declared checked exceptions or subclasses of those checked exceptions. By subset I mean all of, some of, or none of.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in overriding method u cant throw any broader checked exception.
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Barry!
But why is it so?

Sachin Dimble.
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An overriding method should throw only a subset/subclass of the checked exceptions declared by the original method.

By declaring the exceptions in the throws clause, you make sure that the calling program will handle those exceptions.




The above code will not compile. Let us assume that it compiles fine. Now if you assign a Child object to a Parent reference and invode the fetchInfo() method using the Parent reference, the compiler will force you to handle the IOException declared by the Parent class. But, at run time, the fetchInfo() method of the Child class will be called and in all probability the method may throw a ClassNotFoundException (and the caller never handled it). Hence, if the above code was allowed to go scott free by the compiler, the JVM would never attain salvation


Hope I answered you



Arvind
 
Sachin Dimble
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx A Ton Arvind!!

Sachin Dimble.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic