Why does a looser specification in an overriding method compile after the exception specification fo

Posted by Everyone on Stack Overflow See other posts from Stack Overflow or by Everyone
Published on 2010-03-09T07:34:16Z Indexed on 2010/03/09 7:36 UTC
Read the original article Hit count: 281

The code below has an overridden method with a looser exception specification as compared to the method being overridden.

//AnotherMain.java
public class AnotherMain {
    public void dummyMethod( String args[] ) throws IOException{

        throw new IOException();
    }
}

//SubAnotherMain.java
public class SubAnotherMain extends AnotherMain{

    @Override
    public void dummyMethod( String[] args ) throws Exception {
        // To get this to compile, change the above - throws IOException, Exception
        super.dummyMethod(args);
        throw new Exception("This will not compile unless the exception specification has IOException too");
    }

}

Afaik, the overriding method should not compile at all as the looser specification might break substitutability.

Why does it compile after the original exception specification is included in the override? What have I misunderstood?

© Stack Overflow or respective owner

Related posts about exception-specification

Related posts about overriding