• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'throws' at the declaration at the method

 
sura watthana
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.)
 
Sagar Kar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic