1

This question is in continuation from this as suggested by one of the user.

I am using the getIDs function as below to process the id's. CheckValid() will check if the id's is a valid one to be processed, if yes then it will go to the next one updateUsers(). Check valid just checks for a condition and if not it throws an exception. updateUsers() just updates a column if it passes checkValid().

Problem – If I get 4 id's as output from getIDs(), and with the execute(), it process 2 for example and if it fails for 2nd id, it doesn't continue for the rest 2 id's ..I want it to continue so I commented out the "throw $e in the catch block".

Function execute() { 
 for($i=0 ; $i<count($this->getIDs()); $i++) { 
try {
 $this->checkValid();
 $this->updateUsers(); 
} catch(Exception $e) {
  //throw $e;
}
3
  • Help kittens! Don't kill them ! Commented Sep 20, 2011 at 14:34
  • 1
    Aaaand where's the question? Seems like you "solved" it by doing nothing in the catch block? Commented Sep 20, 2011 at 14:36
  • throw will interupt the loop, but suppressing the errors may not be smart. Commented Sep 20, 2011 at 14:36

2 Answers 2

1

have you try a simple continue in the catch block ? didn't test but maybe something like that:

Function execute() { 
 for($i=0 ; $i<count($this->getIDs()); $i++) { 
    try {
     $this->checkValid();
     $this->updateUsers(); 
    } catch(Exception $e) {
     //throw $e;
     continue; // if not working try a continue 2;
    }
  }
} 
Sign up to request clarification or add additional context in comments.

Comments

1

It sounds like you're using exceptions as booleans, I'd suggest avoiding that, as it gets confusing quickly, unless you really need the contents of the exception. See if this makes any sense for your use case (I'll grant, it may not).

// returns true if valid, false otherwise
function checkValid(){
    try {
        // do your validation
        return true;
    } catch (Exception $e) {
        // optional: save the exception in case we want to know about it
        $this->last_error = $e;
        return false;
    }
}

function execute() { 
    for($i=0 ; $i<count($this->getIDs()); $i++) { 
        if($this->checkValid()){
            $this->updateUsers();
        }
        // if you want to do something with an error, simply add an else clause
        // and handle $this->last_error
    }
}

Also, I obviously don't know your code or what exactly you're doing, but looping n times and calling checkValid() and updateUsers() without parameters seems like very poor practice. Much better to, for instance, loop over the list of IDs and check each ID and user in turn, something like this:

foreach($this->getIDs() as $id){
    if($this->checkValid($id)){
        $this->updateUser($id);
    } else {
        // an advantage of this is now we can know exactly which ID failed,
        // because we have the $id variable
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.