my dog learned polymorphism*
The moose likes Mock Exam Errata and the fly likes who is correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "who is correct?" Watch "who is correct?" New topic
Author

who is correct?

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
here in a MockTest :
http://www.jaworski.com/java/certification/
in one question abt overriding this statement is said to be correct:
The overriding method must not throw any exception that may not be thrown by the overridden method
but i think stmt should be "must not throw any checked exception"
CMIW
TIA


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Any method may throw any non-checked exception any time it feels like, with or without declaring it.

The overriding method must not throw any exception that may not be thrown by the overridden method

since the overridden method is completely free to throw a non-checked exception, I think the statement is correct as written.
Just my opinion!

Rob


Rob
SCJP 1.4
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
I have raised QUE for word *any* exception
any includes checked and un-checked exception both but unchecked can be thrown by overriding method.
CMIW
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by ravish kumar:
The overriding method must not throw any exception that may not be thrown by the overridden method

The overridden method must declare any checked exceptions it throws. It may then throw any of these exceptions, as well as subclasses of these exceptions. It may throw any non-checked exceptions with or without declaring them. It may not throw any checked exceptions that it doesn't declare explicitly, or that inherit from those exceptions it declares.
The *overriding* method then, must only throw exceptions that the superclass method can throw. Conversely, it must not throw any exceptions that the supeclass' method cannot throw.
The statement is therefore true as stated!
Rob
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi Rob
Please go throgh these small piece of code :
==================================
1) when overriding method throws UNCHECKED EXCEPTION that may NOT be thrown by overridden method

Here I am throwing an unChecked exception in *overiding* method and it is being compiled.

==========================================
2) when overriding method throws CHECKED EXCEPTION that may NOT be thrown by overridden method

on compiling this we get this error:
D:\JavaTp\Q12_020118_0543.java:15: method01() in Q12_020118_0543 cannot override method01() in Test; overridden method does not throw java.security.GeneralSecurityException
public void method01() throws IOException,
^
1 error
Tool completed with exit code 1

=======================================
3)when overriding method throws CHECKED EXCEPTION that may be thrown by overridden method

Now it compiles buddy.
So I think stmt should be

The overriding method must not throw any CHECKED exception that may not be thrown by the overridden method

Please CMIW
TIA
[ January 18, 2002: Message edited by: ravish kumar ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Your example #2 proves the point!
I think you are interpreting "may" as "may have been", as in "if an exception in the superclass method may have been thrown during the method's execution." That is not the correct interpretation of 'may'.
You should use "allowed to", as in "the superclass method is allowed to throw a particular type of exception based on the declaration in it's throws clause."
In your examples, the superclass method is NOT going to be executed when the subclass method is invoked because you don't have any calls to super.method01(). So it doesn't matter at all if the code in the superclass method might have or might not have thrown an exception if you invoked it. All that matter is the method declaration's "throws" clause.
You should interpret the statement as

The overriding method must not throw any exceptions that may not be thrown by the overridden method

as

The overriding method must not throw any exceptions that are not allowed to be thrown by the overridden method


Rob
[ January 18, 2002: Message edited by: Rob Ross ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi Rob
on secomd thought I think you are right as here they are not talking abt throws clause.
thanks !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: who is correct?