devxlogo

Exception Handling, Inheritance and the Need For Strict Rules

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

See also  Comparing different methods of testing your Infrastructure-as-Code
devxblackblue

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.

About Our Journalist