Why does a looser specification in an overriding method compile after the exception specification fo
- by Everyone
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?