wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 'throws' at the declaration at the method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'throws' at the declaration at the method

sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
Hi,
I have two questions regarding the 'throws' of exception class(es) at the declaration of the method. could somebody just help me out? thanks

the first question is
Must an overriding method declare that it throws the same exception classes as the method it overrides.


the second question is
does the overriding method automatically 'throws' the same exception classes without having to declare them again?




Sura
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Must an overriding method declare that it throws the same exception classes as the method it overrides?"

No. If the overriding method doesn't throw anything itself, then no exception declarations are needed.

"Does the overriding method automatically 'throws' the same exception classes without having to declare them again?"

No. If the overriding method throws any unhandled checked exception, then it must declare it. (Further, the overriding method cannot throw any checked exceptions that are not declared by the overridden method.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sagar Kar
Greenhorn

Joined: Sep 03, 2004
Posts: 25
<i>Must an overriding method declare that it throws the same exception classes as the method it overrides.</i>

Ans: No. But if it intends to throw any exception then it has to be a subclass of the exception that the super class method declares.


Warm regards,<br />Sagar Kar.<br />SCJP 1.4, SCBCD 1.3
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Neo Anderson:
...if it intends to throw any exception...

...any checked exception...

This is what is meant by "the overriding method cannot throw any checked exceptions that are not declared by the overridden method." (Note that if Exception E2 extends Exception E1, then E2 is an instance of E1. So if the overridden method declares that it throws E1, then E2 is also included in this declaration. However, the overridding method must still declare anything it throws.)
[ October 19, 2004: Message edited by: marc weber ]
 
jQuery in Action, 2nd edition
 
subject: 'throws' at the declaration at the method
 
Similar Threads
Overriding and Exceptions
JQ+: Question ID :960680824810
Overriding Methods
Exceptions
Exceptions and overriding