Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Intermediate
Jan 4, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Anil Nair
Thanks for your registration, follow us on our social networks to keep up-to-date