"if super class throws the checked excpetion, base class should also throw the exception"
I assume, you made a typo mistake here and what you wanted to say is "if super class throws the checked excpetion, then sub
class should also throw the exception"
Yes, this is correct.
The overriden method in the subclass can only throw a subset of the checked exception classes(including their subclasses) thrown by the inherited method in the superclass. This means that an overriding method cannot allow more checked exceptions in its throws clause than the inherited method does.
Allowing more checked exceptions in the overriding method would create problems for clients who already deal with the exceptions specified in the inherited method. Such clients would be ill prepared if an object of the subclass (under the guise of polymorphism
) threw a checked exception they were not prepared for.
I hope I have not confused you more#
To summarize, if the super class method throws any checked exception then coresponding sub class method can only throw following type of exceptions:
1. Do not throw any exception
2. Throws all exceptions which are thrown by super class method
3. Throws any exceptions which are sub type of any exceptions thrown by super class method
4. Any run time exception
In all these cases, the code to call this method must be enclosed by try/catch with catch block for all the checked exceptions thrown by super class method.
Just learned a new internet acronym! HTH