title: java: throws RuntimeException
body: <p>How is the below one correct? I expected compiler to tell me to use <code>throws Exception</code> or <code>throws RuntimeException</code></p>
  
  <pre><code>public void method1() throws NullPointerException { 
  
          throw new RuntimeException();
      }
  </code></pre>
  
  <p>why I think its not correct -> Bcoz a NPE is a RTE, but a RTE is not a NPE </p>
  
  <p>How is this correct? I expected compiler to tell me to use <code>throws Exception</code> or <code>throws RuntimeException</code> or <code>throws NumberFormatException</code></p>
  
  <pre><code>public void method2() throws NullPointerException { 
  
  
      throw new NumberFormatException();
  }
  
  
  public void method3() throws Exception { // this is fine, as expected
  
          throw new RuntimeException();
      }
  
  
      public void method4() throws RuntimeException { // this is fine, as expected
  
          throw new NullPointerException();
      }
  
      public void method5() throws Exception { // this is fine, as expected
  
          throw new NullPointerException(); 
      }
  </code></pre>
  
  <p><strong>Answer:</strong></p>
  
  <p>for RTE even if u don't add <code>throws</code> clause to the method, compiler won't say anything</p>
  
  <pre><code>public void method6()  { // no compile time errors!!
  
          throw new NullPointerException(); 
      }
  </code></pre>
  
  <p>But when we explicitly say that '<code>throw new NullPointerException();</code>' , why compiler ignores it?
      It is same as '<code>throw new SQLException()</code>;'
      It is not thrown on runtime say some object was evaluated to null, and invoked an action on that null object.
      Normally a function must declare all the exceptions that it can throw, but RTE's is bypassing it!</p>
  
  <p>RTE's are unchecked exceptions. But when you say throw new RTE, still unchecked?!</p>
  
  <p><strong>Question</strong> - Isn't this a flaw? or please correct me in understanding why is it like that</p>
  
  <ul>
  <li><strong>Update:</strong></li>
  </ul>
  
  <p>Please note that this question is <strong>not about difference between</strong> checked exception and unchecked exception. 
  The question is not about difference between any type of exception or error.</p>
  
  <p>The question is why an explicitly marked RunTimeException is not handled, or left without forcing the compiler to handle it. </p>
  
  <p>eg:</p>
  
  <pre><code>public void methodA() { // methodA is not forced to handle the exception.
          methodB(); 
      }
  
      public void methodB() throws RuntimeException {
  
      }
  </code></pre>