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!
source share