Exception Handling, Inheritance and the Need For Strict Rules

Java has some strict rules for exception handling when it comes to inheritance and overriding of methods.

Consider this example:

 class Base                     {    void amethod() { }}class Derv extends Base{    void amethod() throws Exception { } //compile-time error    public static void main( String s[] )    {        Base b = new Derv();   // line 12        b.amethod();           // line 13    }}


The above code gives an error when compiled. The reason for this is that when the subclass overrides a method of the super class, the method definition in the subclass can only specify all or a subset of the exception classes in the throws clause of the overridden method in the superclass.

What

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: