title: Compile-Time Checking of Exceptions. Case where finally block is throwing unchecked exception implicitly
body: <p>The following code compiles perfectly. And I believe it's because the compiler knows at compile time that the control will go to the finally block and throw the unchecked exception (which is okay and doesn't require to be handled) and it knows that all other exceptions thrown by the code prior to this point are lost. so no need to worry abt them.</p>
  
  <pre><code>try{
       // DoSomething();
  }catch(Exception e){ 
       // Throw checked exception
  }finally{
      // Throw unchecked exception
  }
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>public class TestClass {
  public static void main(String[] args) {
      TestClass c = new TestClass();
      try {
          // Whatever
      } catch (Exception e) {
          throw new FileNotFoundException(); 
      } finally {
          throw new NullPointerException();
      }
  }
  }
  </code></pre>
  
  <p><strong>so far so good until I throw the unchecked exception from a method.</strong></p>
  
  <pre><code>try{
       // DoSomething();
  }catch(Exception e){ 
       // Call a method that throws a checked exception
       // or just throw the checked exception from here
  
  }Finally{
      // Call a method that throw an unchecked exception
  }
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>public class TestClass {
  public static void main(String[] args) {
      TestClass c = new TestClass();
      try {
          //Whatever
      } catch (Exception e) {
           c.m1(); 
               // or just throw it here
               // throw new FileNotFoundException();
      } finally {
          c.m2();
      }
  }
  
  public void m1() throws IOException {
      throw new FileNotFoundException();
  }
  
  public void m2() throws RuntimeException {
      throw new NullPointerException();
  }
  }
  </code></pre>
  
  <p>This code will not compile. It marks c.m1() with an error "Unhandled exception type _ " (eclipse) or "unreported exception _; must be caught or declared to be thrown" (cmd).</p>
  
  <p>It's like it ignored that the <strong>finally</strong> block will throw the LAST exception (which is unchecked) and no need to worry about the one in the catch block even if it was an unhandled checked exception because they are going to be lost anyway! Knowing that m2() is DECLARED to throw specifically an unchecked exception (RuntimeException).</p>
  
  <p>Does anyone have a better explanation about why there's a compilation error in the second code?
  Thanks :)</p>