I have a class like below:
public class MyClass {
public void foo() throws IOException {
System.out.println("Hola");
}
public MyClass() throws IOException {
}
}
As you can see I am declaring IOException
in the throws
clause of the method and the constructor. But I am not throwing that exception anywhere in the body. So it should be a compile time error like when we try to catch an exception that is not being thrown from the try block. But in this case it compiles fine. Can anyone explain the reason behind this behavior?