• 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

Overiding with Exceptions

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Above code gives a compilation error stating
Exception is not compatible with throws clause in Code.amethod()
- overrides Code.amethod in Eclipse

But below code gives no compilation error even though there's no NullPointerExcetion clause in superclass.
It gives the following output:
Slum
Dog
Hello




Can you please explain why is NullPointerException spared ?
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NullPointerException is an Unchecked Exception (not the compile time Exception) whereas Exception is a checked Exception's SuperClass.....
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rule for Overriding is if in superclass method thows a checked exception then the overrideen method must throws that exception or subtype of that exception or it may not throws any exception;
But in case of if subclass overriden method cant thows checked exception if its superclass method doesnot throws Exception
but there is no condition for unchecked exception
NullPointerException is unchecked RuntimeException
Exception is Checked
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.Can a subclass overriding method throw a narrower Exception?
i.e
if super class throws Exception and subclass IOException
 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes it can. you got it
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
N Lv please check your private messages for some administrative matter...
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the above code snippet if
super class throws Exception and subclass IOException
It still gives a compile error, hence forcing the main method of subclass to throw
Exception
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we take this code into consideration

Since the reference is of type Code, so the main method will have to handle Exception and not IOException. This is because the method call will be resolved to amethod of class Code at compile time. Although at runtime amethod in class ScrapCode will be called, but the compiler doesn't know about that. The compiler only knows that the type of ref is Code and amethod in Code throws Exception...
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankit,
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On changing the reference to
ScrapCode ref = new ScrapCode();

It still give compile error
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you throws a checked exception you are bound to either catch it or throws it to JVM
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nikhil you can edit your message using button if you write something wrong or press submit by mistake.

On changing the reference to
ScrapCode ref = new ScrapCode();

It still give compile error


But if you see the error, when the code is Code ref = new ScrapCode();, then the error will be Exception must be caught or thrown, and when the code is ScrapCode ref = new ScrapCode();, then it will say IOException must be caught or thrown. Do you understand the difference now??
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic