title: Why is catching checked exceptions allowed for code that does not throw exceptions?
body: <p>In Java, methods that throw <strong>checked</strong> exceptions (<a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Exception.html" rel="noreferrer">Exception</a> or its subtypes - IOException, InterruptedException, etc) must declare <strong>throws</strong> statement:</p>
  
  <pre><code>public abstract int read() throws IOException;
  </code></pre>
  
  <p>Methods that do not declare <code>throws</code> statement <strong>can't</strong> throw checked exceptions.</p>
  
  <pre><code>public int read() { // does not compile
      throw new IOException();
  }
  // Error: unreported exception java.io.IOException; must be caught or declared to be thrown
  </code></pre>
  
  <p>But catching checked exceptions in safe methods is still legal in java:</p>
  
  <pre><code>public void safeMethod() { System.out.println("I'm safe"); }
  
  public void test() { // method guarantees not to throw checked exceptions
      try {
          safeMethod();
      } catch (Exception e) { // catching checked exception java.lang.Exception
          throw e; // so I can throw... a checked Exception?
      }
  }
  </code></pre>
  
  <p>Actually, no. It's a bit funny: compiler knows that <strong>e</strong> is not a checked exception and allows to rethrow it. Things are even a bit ridiculous, this code does not compile:</p>
  
  <pre><code>public void test() { // guarantees not to throw checked exceptions
      try {
          safeMethod();
      } catch (Exception e) {        
          throw (Exception) e; // seriously?
      }
  }
  // Error: unreported exception java.lang.Exception; must be caught or declared to be thrown
  </code></pre>
  
  <p>The first snippet was a motivation for a question. </p>
  
  <p>Compiler knows that checked exceptions can't be thrown inside a safe method - so maybe it should allow to catch only unchecked exceptions?</p>
  
  <hr/>
  
  <p>Returning to the <strong>main question</strong> - are there any reasons to implement catching checked exceptions in this way? Is it just a flaw in the design or am I missing some important factors - maybe backward incompatibilities?  What could potentially go wrong if only <code>RuntimeException</code> were allowed to be catched in this scenario? Examples are greatly appreciated.</p>