Search code examples
phpexceptionnamespacesphp-5.3

Exception Thrown In Parent Class Not Caught In Child Class


This error is happening when using the Facebook PHP SDK but I actually think it's a general error.

When I run this code, everything works fine and the exception is caught:

$facebook = new Facebook('appId'=>APP_ID,'secret'=>APP_SECRET);
try {
    $user_profile = $facebook->api('/me','GET');
    echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
    $login_url = $facebook->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
}

But when I run this code:

$facebook = new Facebook_Extended('appId'=>APP_ID,'secret'=>APP_SECRET);
$facebook->api_extended();

With the extended class's method looking like this:

// Overrides parent::api()
public function api() {
    try {
        $user_profile = parent::api('/me','GET');
        echo "Name: " . $user_profile['name'];
    } catch(FacebookApiException $e) {
        $login_url = $this->getLoginUrl(); 
        echo 'Please <a href="' . $login_url . '">login.</a>';
    } 
}

I get an "Uncaught OAuthException" error.

Any ideas why the exception is not able to be caught in the child class?


Solution

  • I didn't realize this was an issue, but this class is in a namespace, so I needed to add a "\" to my Exception:

    catch (\FacebookApiException $e)
    

    http://onehackoranother.com/logfile/2009/01/php-5-3-exception-gotcha