title: How to efficiently use try...catch blocks in PHP
body: <p>I have been using try..catch blocks in my PHP code, but I'm not sure if I've been using them correctly.</p>
  
  <p>For example, some of my code looks like:</p>
  
  <pre><code> try {
        $tableAresults = $dbHandler-&gt;doSomethingWithTableA();
        $tableBresults = $dbHandler-&gt;doSomethingElseWithTableB();
   } catch (Exception $e) {
        return $e;
   }
  </code></pre>
  
  <p>So I'm grouping multiple database operations in the same try/catch block because if any exception occurs in any of the transaction, I will be able to handle it.</p>
  
  <p>I'm doing it that way because I think that it's more readable and efficient than:</p>
  
  <pre><code> try {
         $tableAresults = $dbHandler-&gt;doSomethingWithTableA();
   } catch (Exception $e) {
         return $e;
   }
   try {
         $tableBresults = $dbHandler-&gt;doSomethingWithTableB();
   } catch (Exception $e) {
         return $e;
   }
  </code></pre>
  
  <p>Although, I'm not sure if what I'm doing is a good practice or just a lazy way to catch exceptions.</p>
  
  <p>My assumption is that only if an exception requires special handling, it should have its own try/catch block, otherwise grouping them in the same try/catch should be ok. </p>
  
  <p>So my question(s) are:</p>
  
  <p>Is there any advantage of using try/catch blocks per database transaction? or can I still group multiple database transactions in the same try/catch block with no problem at all?</p>
  
  <p>Is it ok to nest try/catch blocks?
  Thanks!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The return statement was primarily for demonstration purposes only, but I'm also using returns in <code>catch()</code> because I'm making an AJAX request to that method, and Javascript is expecting a JSON object, then if an exception occurs I return an empty JSON encoded array. I just thought that It wouldn't add any value to put specific code in my example.</p>