php - try, catch, and retry

后端 未结 6 2133
一向
一向 2020-12-23 19:49

Sometimes my code breaks and it is out of my control

How would I do the following?

try {
//do my stuff
}
catch {
//sleep and try again
}
         


        
6条回答
  •  太阳男子
    2020-12-23 20:32

    Simply :

    function doSomething($params, $try = 1){
        try{
            //do something
            return true;
        }
        catch(Exception $e){
            if($try <5){
                 sleep(10);
                 //optionnaly log or send notice mail with $e and $try
                 doSomething($params, $try++);
            }
            else{ 
                 return false;
            }
        }
    }
    

提交回复
热议问题