Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:11:00+00:00 2026-05-22T23:11:00+00:00

this is more of a conceptual question. Is it aceptable/good programming mixing do/while loops

  • 0

this is more of a conceptual question.

Is it aceptable/good programming mixing do/while loops with try/catch?

ex:

<?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’m asking is that I read somewhere that mixing both is bad practice. You should create your own exceptions and “throw” them instead. But isn’t that a bit of an overkill?

EDIT:
To explain the scenario where this question emerged, imagine a robot that has to follow a line. The robot calculates its position (X position, Y poistion, Z position and “postural” position) everytime he takes a step and sends the information to a server. Server doublechecks this information. If any anomaly is found, servers sends a “stop” signal to the robot. The robot stops, recalculates its position, resends information and waits for a “go” signal.

This loop example was based on the data feed received from the robot. If something went wrong, (like a broken wireless link, or an obstacle, or a misstep) the robot must stop to prevent going astray, or falling, or anything. We aren’t interested in knowing what went wrong, or why it went wrong, just that it went wrong (or not). (that is actually taken care in another part of the code, not PHP based, the Debug Module).

EDIT 2:
Like everyone pointed out, seems the right approach is to properly raise/handle the exceptions.
Since everyone seems to agree in this, I don’t know who to award the “right answer” tick.
I will wait a few days and give it to the one with more votes if that’s ok!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:11:01+00:00Added an answer on May 22, 2026 at 11:11 pm

    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 block.

    Catching the base exception type and swallowing it is a bad practice in any language, PHP or otherwise. It is best to only catch specific exception types, and let the rest fall through the catch block.

    For example, if you were accessing a file, and caught the FileNotFound exception (if there is such a thing in PHP), that would be OK.

    But if you catch all exception types, then you’re going to catch things like OutOfMemoryException. This would be fine, if you properly handled it, and made sure it wasn’t going to throw again. But in your case, you’re probably not “doing the right thing” for that case, so you should just let it fall through.

    The reason you don’t want to eat exceptions (without logging them) is that you won’t be able to debug your code if it ever goes to a locked down production environment. If the exception is caught, then the user will just see buggy behavior, and won’t be able to file a bug report that gives you enough information to debug it. They might be able to give you repro steps, but that is pretty unreliable. This is also pretty much worthless for an intermittant problem.

    If you eat the exception, you’re basically throwing away the most useful data there is for debugging.

    Things that make this less true:

    • Sometimes in top-level web code, you will want to catch the exception, log it, then swallow it so the user doesn’t see it (but return from the function, so you don’t execute more code while in a buggy state). This makes it so the user doesn’t see revealing information about your code (the stack trace).
    • If the language you are doing exception handling in has poor support for exceptions, and doesn’t have derived exception types. I don’t know a lot about PHP, so this may or may not be true in this language.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is more a conceptual question. Consider you have a php framework that runs
This is more of a conceptual question and not about the underlying programming technique.
This is more of a business-oriented programming question that I can't seem to figure
I have an interesting situation here this time. This more of a conceptual question,
This is more of a conceptual question than anything. I have a base class
This was more of a conceptual question, but I'll provide a particular instance where
This is more of an academic inquiry than a practical question. Are there any
This is more of an generic XML Schema question, but if and how do
This is more an observation than a real question: MS-Access (and VBA in general)
This is more of a syntax question I'm trying to write a store procedure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.