Skip to content

Instantly share code, notes, and snippets.

@betawax
Created June 25, 2014 06:16
Show Gist options
  • Save betawax/bd7b84ef8cb111b090a7 to your computer and use it in GitHub Desktop.
Save betawax/bd7b84ef8cb111b090a7 to your computer and use it in GitHub Desktop.
Repeating try / catch block
$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