Forums Register Login

'throws' at the declaration at the method

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
"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.)
+Pie Number of slices to send: Send
<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.
+Pie Number of slices to send: Send
 

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 ]
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1051 times.
Similar Threads
Exceptions
Exceptions and overriding
Overriding and Exceptions
Overriding Methods
JQ+: Question ID :960680824810
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:31:16.