devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.