Exception Handling with Method Overriding in JavaThere are many rules if we talk about method overriding with exception handling. Some of the rules are listed below:
If the superclass method does not declare an exceptionRule 1: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception.Let's consider following example based on the above rule. TestExceptionChild.java Output: Rule 2: If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but can declare unchecked exception.TestExceptionChild1.java Output: If the superclass method declares an exceptionRule 1: If the superclass method declares an exception, subclass overridden method can declare the same subclass exception or no exception but cannot declare parent exception.Example in case subclass overridden method declares parent exceptionTestExceptionChild2.java Output: Example in case subclass overridden method declares same exceptionTestExceptionChild3.java Output: Example in case subclass overridden method declares subclass exceptionTestExceptionChild4.java Output: Example in case subclass overridden method declares no exceptionTestExceptionChild5.java Output: Next TopicCustom Exception |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India