title: java conditional operator for exceptions
body: <pre class="lang-java prettyprint-override"><code>private static int testReturn(final boolean flag) {
      return flag ? 1 : 2;
  }
  
  private static void testThrow1(final boolean flag)
      throws IOException, SQLException {
  
      if (flag ) {
          throw new IOException("IO");
      } else {
          throw new SQLException("SQL");
      }
  }
  </code></pre>
  
  <p>When I tried to change it with <code>?:</code> operator,</p>
  
  <pre class="lang-java prettyprint-override"><code>private static void testThrow2(final boolean flag)
      throws
          //IOException, SQLException, // not even required
          Exception {                  // compiler wants this.
  
      throw flag ? new IOException("IO") : new SQLException("SQL");
  }
  </code></pre>
  
  <p>Is it normal?</p>
  
  <p><strong>Gratitudes</strong></p>
  
  <p>I actually encountered this code when I was preparing a presentation for Java 7 features such as multiple-catch and typed rethrowal. Interesting. Thank you all for those good answers. I learned a lot.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Java 7 has been improved for type specific rethrow, right?</p>
  
  <pre><code>void throwIoOrSql() throws IOException, SQLException {
  }
  
  void rethrowIoAndSql() throws IOException, SQLException {
      try {
          throwIoOrSql();
      } catch (Exception e) {
          throw e; // Ok with Java 7
      }
  }
  </code></pre>
  
  <p>It's somewhat silly the compiler can't see those obvious case.</p>
  
  <pre><code>throw flag ? new IOException("io") : new SQLException("sql"); // obvious, isn't it?
  </code></pre>