Skip to main content

Intersection of exception in class or interface hierarchy

Question?

Q1. Will below program compile?


public class ExceptionTestForInterface implements Type3
{

  public void f()
  {
    System.out.println("Hello World");
  }
  
  public static void main(String[] args)
  {
    Type3 type = new ExceptionTestForInterface();
    type.f();
  }

}


interface Type1 {
  void f() throws CloneNotSupportedException;
}

interface Type2 {
  
  void f() throws InterruptedException;
  
}

interface Type3 extends Type1, Type2{
  

}

Ans => Yes, It will compile and successfully print the "Hello World"


Remember

The set of checked exceptions that a method can throw is the intersection of the sets of checked exceptions that it is declared to throw in all applicable types.



Comments