• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overiding with Exceptions

 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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 ?
 
sakthi moorthy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NullPointerException is an Unchecked Exception (not the compile time Exception) whereas Exception is a checked Exception's SuperClass.....
 
Debasis behera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Mark Uppeteer
Ranch Hand
Posts: 159
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it can. you got it
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Ankit,
 
Nikhil Shah Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic