2

Which is the right way to implement php exception (try{}catch(){}) in a foreach loop that looks like this:

foreach ($apis as $api)
{                 
     $api = '_'.$api;
     $searchResults[$api] = $this->$api($parameters);
}

I want to implement the php exceptions for if one of the $this->api(); returns an error message, than catch it and do a if inside the catch to display the right message for the error message returned.

Edit: Also, when capturing the error and if the error message is 1 (for example) is it a good way to do:

$searchResults['api'] = $this->_api($parameters);

so it tries to do the function again and see if this time it brings valid data?

1
  • Why just try once? If you want to have a retry policy, you will have to implement it in a way that shows your policy. For instance, you can put a while-loop with a counter that will ensure at least five retries before declaring the cause as lost. Commented Jan 15, 2012 at 13:21

3 Answers 3

4
foreach ($apis as $api)
{                 
     $api = '_'.$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(ParameterException $e) {
         // parameterexception handling here
         echo "A ParameterException was thrown";
     }
     catch(Exception $e) {
         // All other exceptions
         echo "Some other Exception was thrown";
     }
}

You can differentiate between more Exception-Types as well.

Sign up to request clarification or add additional context in comments.

6 Comments

Or use 1 catch block and use instanceof: catch( Exception $e ) { if( $e instanceof ParameterException ) { echo 'some parameterexception was thrown'; } }
I've just added this, I will let you know if it works. I need to implement it because sometime I just don't know why one of the apis won't return valid message/connect and need to catch that error and try and do the function again
having for example this exception: ApiNameLookupError, could I just grab the Api and do the $searchResults['api'] = $$this->_api($parameters); ?
@w0rldart I don't understand what you want to do. When you want to catch an ApiNameLookupError you simply replace the names in my example. Whenever an exception is thrown you immediatly jump into the first possible catch-block.
I just want to filter the exception, if from Api1 do $searchResults['api1'] = $$this->_api1($parameters); if from Api2 do api2 function
|
3

Since the catch block will be executed only in case of an exception, it really makes no difference if you wrap the for-each loop inside the try block, or if you put the try-catch inside the loop's body.

You should take whatever adds more clarity. However, doing it inside the loop will enable you to handle more specific exceptions relevant to the loop's body if the need arises in the future.

Also, since exceptions are typed, you don't need to do an if, just put different catch clauses:

try {
    :
} catch (FirstExceptionType $e) {
    :
} catch (SecondExceptionType $e) {
    :
}

1 Comment

No problem. Glad to have been of help.
1

The code you use will always fail, because you are trying to use a variable ($this->$api($params);) as a function. How ever, you could implement your try-catch as follows:

foreach ($apis as $api)
{                 
     $api = '_'.$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(Exception $e) {
         // handle exception
     }
}

You can also handle multiple Exceptions of different types by adding another catch() with another Exception class inside it, like:

foreach ($apis as $api)
{                 
     $api = '_'.$api;
     try {
         $searchResults[$api] = $this->$api($parameters);
     }
     catch(OtherException $e) {
         // Handle it
     }
     catch(Exception $e) {
         // Handle it
     }

}

1 Comment

This will not work as Exception will already handle any exceptions. The second catch will not be reached.

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.