title: What is the criteria for throwing exceptions in subclass
body: <p>What I have known till now is that a subclass if overriding a superclass method should throw the same exception or a subclass of the exception.</p>
  
  <p>For example:</p>
  
  <p><strong>This is correct</strong></p>
  
  <pre><code>class SuperClass {
      public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
   String signature = "(String, Integer[])";
   System.out.println(str + " " + signature);
   return 1;
   }
  }
  
  public final class SubClass extends SuperClass {
      public int doIt(String str, Integer... data) throws ArrayIndexOutOfBoundsException {
          String signature = "(String, Integer[])";
          System.out.println("Overridden: " + str + " " + signature);
          return 0;
      }
  
      public static void main(String... args) {
          SuperClass sb = new SubClass();
          try {
              sb.doIt("hello", 3);
          } catch (Exception e) {
          }
      }
  }
  </code></pre>
  
  <p><strong>This is Incorrect</strong></p>
  
  <pre><code>class SuperClass {
      public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
   String signature = "(String, Integer[])";
   System.out.println(str + " " + signature);
   return 1;
   }
  }
  
  public final class SubClass extends SuperClass {
      public int doIt(String str, Integer... data) throws Exception {
          String signature = "(String, Integer[])";
          System.out.println("Overridden: " + str + " " + signature);
          return 0;
      }
  
      public static void main(String... args) {
          SuperClass sb = new SubClass();
          try {
              sb.doIt("hello", 3);
          } catch (Exception e) {
          }
      }
  }
  </code></pre>
  
  <p><strong>But my question is, why this code block is considered correct by compiler?</strong></p>
  
  <pre><code>class SuperClass {
      public int doIt(String str, Integer... data)throws ArrayIndexOutOfBoundsException{
   String signature = "(String, Integer[])";
   System.out.println(str + " " + signature);
   return 1;
   }
  }
  
  public final class SubClass extends SuperClass {
      public int doIt(String str, Integer... data) throws RuntimeException {
          String signature = "(String, Integer[])";
          System.out.println("Overridden: " + str + " " + signature);
          return 0;
      }
  
      public static void main(String... args) {
          SuperClass sb = new SubClass();
          try {
              sb.doIt("hello", 3);
          } catch (Exception e) {
          }
      }
  }
  </code></pre>