Using Try / catch with Do / While

It is rather a conceptual question.

Is this an acceptable / good mix of programming to do / while loops with try / catch?

Example:

<?php function main() { $failure = 0; do { $failure += foo(); $failure += bar(); ... if ($failure) { return $failure; } } while ($failure == 0); } function foo() { try { echo 'DO FOO STUFF<br />'; return 0; } catch (Exception $e) { return 1; } } function bar() { try { echo 'DO BAR STUFF<br />'; return 0; } catch (Exception $e) { return 1; } } ?> 

The reason I ask is because I read somewhere that mixing is bad practice. You must create your own exceptions and throw them instead. But isn't that too much?

EDIT: To explain the scenario for this question, imagine a robot that should follow a line. The robot calculates its position (position X, Y poistion, position Z and "postural" position) every time it takes a step and sends information to the server. The server duplicates this information. If any anomaly is detected, the server sends a stop signal to the robot. The robot stops, recounts its position, transmits information and waits for a “go” signal.

This example loop was based on a data channel received from a robot. If something went wrong (for example, a broken wireless connection, or an obstacle, or failures), the robot must stop so as not to go astray, or fall, or anything else. We are not interested in knowing what went wrong, or why it went wrong, it just went wrong (or not). (in fact, this will be taken care of in a different part of the code, and not based on PHP, the debugging module).

EDIT 2: As everyone pointed out, it seems like the right approach is to raise / handle exceptions correctly. Since everyone seems to agree on this, I don’t know to whom to award the “right answer”. I will wait a few days and give it to someone who has more votes, if that's good!

+4
source share
3 answers

There is no reason to avoid mixing try/catch with do/while .

But the example you give has less problems with do/while and more with swallowing exceptions in the try/catch .

Capturing the basic type of exception and swallowing is bad practice in any language, PHP or otherwise. It is best to catch only certain types of exceptions, and the rest through the catch block.

For example, if you accessed a file and caught a FileNotFound exception (if there is such a thing in PHP), this will be fine.

But if you catch all types of exceptions, you will catch things like OutOfMemoryException . It would be nice if you processed it correctly and made sure that it was not going to be thrown again. But in your case, you probably aren’t “doing the right thing” for this case, so you just have to let it fail.

The reason you don’t want to have exceptions (without registering them) is because you cannot debug your code if it ever gets into a blocked production environment. If an exception is caught, the user will simply see erroneous behavior and will not be able to report an error, which will give you enough information to debug it. They may be able to give you repeated steps, but this is pretty unreliable. It is also pretty unhelpful for an interrupt problem.

If you eat an exception, you basically discard the most useful data for debugging.

Things that make this less true:

  • Sometimes in the top-level web code you need to catch an exception, register it, then catch it so that the user does not see it (but returns from the function, so you do not execute more code during the buggy state). This ensures that the user does not discover information about your code (stack trace).
  • If the language in which exception handling is performed has poor exception support and does not have derived exception types. I don't know much about PHP, so this may or may not be true in this language.
+3
source

If you want to catch the exceptions and just report the error, then everything will be fine. You will need to do something like

  $failure &= bar(); 

or you lose the foo () failure condition.

If you want to be able to act by exception further up the call stack, then you better leave it alone or at least reinstall it so that you can catch it where you want to handle it.

+1
source

There is no general reason to avoid using try / catch inside functions called inside a do / while loop.

Depending on what you are actually doing inside the loop, you may be able to catch exceptions inside the loop and exit the returned error condition or wrap the while while loop in a try / catch block.

You have to decide how to do this in any particular case according to what the exceptions mean for your code:

  • Is it possible to recover an exception?
  • What actions should be taken in response to the generated exception?
  • In what condition should your program remain after any specific exception has been sent and processed?

In your example, bar() will execute even if foo() throws an error. This may or may not be what you want. Capturing exceptions outside the loop will not allow this option. In addition, the loop will not end if bar() returns false , if foo() subsequently returns true - this is probably an error. Capturing any exceptions outside the loop means the loop will be aborted at the point of error. Again, this may or may not be desirable depending on your application.

In addition, it is likely that different exceptions should be handled differently. It is important to note that if you catch the base type of the exception, and not the more specific derived type, you lose the ability to do this. Considering more specific exceptions (including custom exceptions) will help you:

  • Make sure that you do not accidentally swallow unexpected exceptions that you did not want to catch.
  • It has several different script errors handled by the same catch block (by throwing the same exception for each of these script errors).
+1
source

All Articles