Created
June 25, 2014 06:16
-
-
Save betawax/bd7b84ef8cb111b090a7 to your computer and use it in GitHub Desktop.
Repeating try / catch block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$sleep = 1; | |
$attempts = 3; | |
for ($attempt = 0; $attempt < $attempts; $attempt++) | |
{ | |
try | |
{ | |
// ... | |
} | |
catch (Exception $e) | |
{ | |
Log::warning('Triggered exception at attempt #'.($attempt+1).'.'); | |
if ($attempt < $attempts - 1) | |
{ | |
sleep($sleep); | |
} | |
else | |
{ | |
Log::error('Giving up after '.($attempt+1).' attempts.'); | |
throw new Exception($e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment