开发者

PHP Error Message Help and Try/Catch

I am trying to create a function that deletes a user from Constant Contact. This function calls a wrapper class for constant contact, and it works, but if you feed it an email address that does not exist on their site it comes back with a catchable fatal error.

I am new to try/catch and I'm not quite getting where to place that so that I can create a graceful error message instead of the Catchable Fatal Error I'm getting. Below is my current code:

function ccdeleteuser($emailaddress)
{
//this code accesses the constant contact wrapper class to delete a user based on email
$ConstantContact = new ConstantContact("basic", "apikey", "usr", "pwd");
$SearchContact = $ConstantContact->searchContactsByEmail($emailaddress);
$DeleteContact = $ConstantContact->deleteContact($SearchContact[0]);  
}

$emailtotry = "test@test.com";  //this is email is NOT in Constant Contact
ccdeleteuser($emailtotry);

Right now if I run this I get the following error:

Catchable fatal error: Argument 1 passed to ConstantContact::deleteContact() must be an instance of Contact, null given, called in [path to my page] on line 19 and defined in [path to constant开发者_JS百科 contact php wrapper file] on line 214

Any help is appreciated!


The right way to do this is to test for null first:

function ccdeleteuser($emailaddress)
{

    $ConstantContact = new ConstantContact("basic", "apikey", "usr", "pwd");
    $SearchContact = $ConstantContact->searchContactsByEmail($emailaddress);
    // first makes sure that the 0 index of of SearchContact is accessible at all
    // then it ensures that only something "truthy" will trigger delete -- this
    // means that if $SearchContact[0] is null, the function won't try to delete
    if( $SearchContact && isset( $SearchContact[0] ) && $SearchContact[0])
        $DeleteContact = $ConstantContact->deleteContact($SearchContact[0]);  
    else
        echo "Can't do nothin'"; // do something useful?
}

With try... catch, you might make it look like this:

function ccdeleteuser($emailaddress)
{

    $ConstantContact = new ConstantContact("basic", "apikey", "usr", "pwd");
    $SearchContact = $ConstantContact->searchContactsByEmail($emailaddress);
    try
    {
        // keep this... it is still useful
        if( $SearchContact && isset( $SearchContact[0] ) && $SearchContact[0])
            $DeleteContact = $ConstantContact->deleteContact($SearchContact[0]);  
        else
            echo "Can't do nothin'";
    }
    catch( Exception $e )
    {
        // I'm making up a function "log" which will record that an error 
        // has taken place. It is a good idea to always log all exceptions
        // so that you don't accidentally obfuscate something important
        log( $e->getMessage() );
        // do something useful
    }
}

As a general note, it is always best to act proactively to prevent exceptions and not simply catch them after the fact. I'll even go so far as to say that you should consider it a rule to always do everything in your power to prevent the possibility of an exception and only use try... catch as a last possible resort.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜