代码之家  ›  专栏  ›  技术社区  ›  user1765876

致命错误停止在PHP中执行

  •  1
  • user1765876  · 技术社区  · 12 年前

    我有以下PHP代码

    foreach($arr as $u){
        //code runs for users
        $u->fetchFriends();
    }
    
    function fetchFriends(){
    
    $sparam = array('method' => 'fql.query', 
                    'query' => $fql, 
                    'callback' => '', 
                    'access_token' => $fbtoken);
    
        try{
            $fqlResult = $facebook -> api($sparam);
            }
        catch(Exception $e) {
            echo 'There is issue with Token';
        }
    }
    

    问题是,如果FBAPI抛出异常,那么进程就会停止,foreach循环中的下一个用户不会被执行。我希望即使它抛出错误,foreach循环也应该为所有用户运行。 这可能吗?

    1 回复  |  直到 12 年前
        1
  •  0
  •   larsAnders    12 年前

    您需要使用FacebookApiException捕获块捕获这些异常:

    function fetchFriends(){
    
    $sparam = array('method' => 'fql.query', 
                    'query' => $fql, 
                    'callback' => '', 
                    'access_token' => $fbtoken);
    
        try {
            $fqlResult = $facebook -> api($sparam);
    
        } catch (FacebookApiException $e) {//catch FB
            echo 'There is an issue with the Token';
    
        } catch(Exception $e) {//catch PHP
            echo $e->getMessage().' in '.$e->getFile().' on line '.$e->getLine();
        }
    }
    
    推荐文章