Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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
Anil Nair
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap