Gery
Gery

Reputation: 147

Validity of using more queries on one try catch?

Is valid to use e.g. two query in one try-catch block?

try
{
   $query1 = prepare("SELECT * FROM tbl1...");
   $query2 = prepare("SELECT * FROM tbl2...");
   $query1 = execute();
   $query2 = execute();
}catch (PDOException $e)
{
   print "Error!: " . $e->getMessage();
   return false;
}

OR, it should be:

try
{
   $query1 = prepare("SELECT * FROM tbl1...");
   $query1 = execute();
}catch (PDOException $e)
{
   print "Error!: " . $e->getMessage();
   return false;
}


try
{
   $query2 = prepare("SELECT * FROM tbl2...");
   $query2 = execute();
}catch (PDOException $e)
{
   print "Error!: " . $e->getMessage();
   return false;
}

If both method are accepted and in use are there any differenc between them? (Speed?) Because I have to do more query in the index but I dont want to separet all

Upvotes: 1

Views: 1273

Answers (5)

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

I'd say use a single try-catch block as much as possible. If your second query depends on the success of your first query, the second won't be executed if the first throws an exception.
A series of try-catch blocks doesn't make for very readable code, to my eye. A sizable try-catch is OK, as long as you're not going all pokemon-style (gotta catch 'em all)... By that I mean that it's ok to perform 5 queries inside a single try-block, but when you're processing the result of each query in that same block, you're taking things too far.

If you're wondering which is the most efficient approach: catch-blocks are triggered by an instance of the Exception class being thrown. The code inside the catch block is expected to deal with that error, so the script needn't fail completely, and normal service can be resumed.
If you use several try-catch blocks, odds are that snippet of code might throw several Exception instances (PDOException in this case). However "minor" the overhead is, creating a new instance isn't free. If, at the end of the ride you still want to present the user with the data of the queries that were successful and show a notice that some data wasn't retrieved successfully, you need multiple try-catches, if you want to show either everything, or nothing, use 1 try-catch block, and use Exception instance to work out what query failed and present the user with the appropriate error message (or just tell them it's all gone wrong)

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72729

Both are perfectly valid. As others have stated it's all about the use case.

The only thing I want to add to it is that you may also want to use a transaction. That way you can rollback if for example if the second query fails. Note this is also depended on the use case.

Upvotes: 2

Lix
Lix

Reputation: 48006

If you need different behaviors according to which command failed then you'll need to wrap each (pair) of commands in their own try/catch block.

If all you are worried about is some error happening from any line, then you can wrap all the commands together and handle all exceptions as the same.

Upvotes: 2

Ivo Pereira
Ivo Pereira

Reputation: 3500

It kind of differs in what's your propuse. If one query is dependant of each other, so they should be in on try-catch. If not, if you don't need data from the other and if you think page can go along without the information of the first try-catch, then you can continue your queries.

At least I think this way.

Upvotes: 1

Eregrith
Eregrith

Reputation: 4366

Both method are accepted but the first one is more readable. One try catch for the same group of functions is enough.

If one of your calls fails it will stop the bloc execution and go directly to the catch, so putting all in the same try works.

I suspect that in the second way, the compiler might recognize the code in the two catchs as being identical and optimize it as the first one but this should be tried out.

Upvotes: 2

Related Questions