Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently use try...catch blocks in PHP

I have been using try..catch blocks in my PHP code, but I'm not sure if I've been using them correctly.

For example, some of my code looks like:

 try {       $tableAresults = $dbHandler->doSomethingWithTableA();       $tableBresults = $dbHandler->doSomethingElseWithTableB();  } catch (Exception $e) {       return $e;  } 

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.

I'm doing it that way because I think that it's more readable and efficient than:

 try {        $tableAresults = $dbHandler->doSomethingWithTableA();  } catch (Exception $e) {        return $e;  }  try {        $tableBresults = $dbHandler->doSomethingWithTableB();  } catch (Exception $e) {        return $e;  } 

Although, I'm not sure if what I'm doing is a good practice or just a lazy way to catch exceptions.

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.

So my question(s) are:

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?

Is it ok to nest try/catch blocks? Thanks!

EDIT

The return statement was primarily for demonstration purposes only, but I'm also using returns in catch() 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.

like image 437
ILikeTacos Avatar asked Jul 09 '13 13:07

ILikeTacos


People also ask

Which is the correct way to use catch block in PHP?

Catch: This block of code will be called only if an exception occurs within the try code block. The code within your catch statement must handle the exception that was thrown. Finally: In PHP 5.5, the finally statement is introduced. The finally block may also be specified after or instead of catch blocks.

When should I use try catch PHP?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

Can we use multiple try catch in PHP?

PHP has an exception model similar to that of other programming languages. An exception can be throw n, and caught (" catch ed") within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch or finally block.

Is try catch efficient?

try catch blocks have a negligible impact on performance but exception Throwing can be pretty sizable, this is probably where your coworker was confused.


1 Answers

Important note

The following discussion assumes that we are talking about code structured as in the example above: no matter which alternative is chosen, an exception will cause the method to logically stop doing whatever it was in the middle of.


As long as you intend to do the same thing no matter which statement in the try block throws an exception, then it's certainly better to use a single try/catch. For example:

function createCar() {     try {       install_engine();       install_brakes();     } catch (Exception $e) {         die("I could not create a car");     } } 

Multiple try/catch blocks are useful if you can and intend to handle the failure in a manner specific to what exactly caused it.

function makeCocktail() {     try {         pour_ingredients();         stir();     } catch (Exception $e) {         die("I could not make you a cocktail");     }      try {         put_decorative_umbrella();     } catch (Exception $e) {         echo "We 're out of umbrellas, but the drink itself is fine"     } } 
like image 88
Jon Avatar answered Sep 27 '22 21:09

Jon