title: How to ignore function calls throwing a RuntimeExceptions in Java?
body: <p>I'm supposed to use <code>tapestry-json</code> as my JSON-API in Java:</p>
  
  <pre><code>import org.apache.tapestry5.json.JSONObject;
  import org.apache.tapestry5.json.JSONArray;
  </code></pre>
  
  <p>Unfortunately there is no <code>optJSONObject(int)</code>-method for reading <code>JSONObject</code>s from a <code>JSONArray</code>. The method <code>getJSONObject(int)</code> Throws a <code>RuntimeException</code></p>
  
  <blockquote>
    <p>... if there is no value for the index or if the value is not a JSONObject.</p>
  </blockquote>
  
  <p>Now I'd like to ignore all those entries. Thus I wrapped it in a try-catch-block.</p>
  
  <pre><code>JSONArray arr = new JSONArray("...");
  
  for(int i = 0; i &lt; arr.length(); i++) {
  
    JSONObject currentEntry;
    try {
      // RuntimeException is thrown, if there is no value for the index or if the value is not a JSONObject.
      currentEntry = arr.getJSONObject(i);
    } catch (RuntimeException e) {
      // Ignore this entry
      continue;
    }
    /*
    if( ... currentEntry ... ) {
  
    }
    */
  }
  </code></pre>
  
  <p>To me, this solution appears heavier that it probably has to be. Is there a more elegant way to ignore RuntimeExceptions?</p>