| Author |
Overiding with Exceptions
|
Nikhil Shah Jain
Ranch Hand
Joined: Apr 21, 2009
Posts: 31
|
|
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
Joined: Oct 17, 2007
Posts: 54
|
|
|
NullPointerException is an Unchecked Exception (not the compile time Exception) whereas Exception is a checked Exception's SuperClass.....
|
 |
Debasis behera
Greenhorn
Joined: Oct 26, 2009
Posts: 14
|
|
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
|
java
|
 |
Nikhil Shah Jain
Ranch Hand
Joined: Apr 21, 2009
Posts: 31
|
|
Ok.Can a subclass overriding method throw a narrower Exception?
i.e
if super class throws Exception and subclass IOException
|
 |
Mark Uppeteer
Ranch Hand
Joined: Mar 02, 2004
Posts: 159
|
|
yes it can. you got it
|
I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
N Lv please check your private messages for some administrative matter...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Nikhil Shah Jain
Ranch Hand
Joined: Apr 21, 2009
Posts: 31
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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
Joined: Apr 21, 2009
Posts: 31
|
|
Ankit,
|
 |
Nikhil Shah Jain
Ranch Hand
Joined: Apr 21, 2009
Posts: 31
|
|
On changing the reference to
ScrapCode ref = new ScrapCode();
It still give compile error
|
 |
Debasis behera
Greenhorn
Joined: Oct 26, 2009
Posts: 14
|
|
if you throws a checked exception you are bound to either catch it or throws it to JVM
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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??
|
 |
 |
|
|
subject: Overiding with Exceptions
|
|
|