Try-Catch没有捕获自定义异常类型

时间:2011-04-05 17:18:42

标签: php syntax try-catch

这是一个非常简单的问题,而且公然是我自己的新状态让我退缩。道歉。

为什么这段代码不起作用?

    try
    {
        create_account($accountXML);
        echo "<p>Successfully created your account.</p>";
        try
        {
            create_page($pageXML,$base64_credentials);
            echo "<p>Successfully created your page!</p>";
        }
        catch (exception $e){ echo "<p>$e</p>"; }
    }
    catch(exception $e)
    {
        echo "<p>$e</p>";           
    }
    catch(emailInUseException $e)
    {
        echo "<p>Error: Email already in use. Could not create account.</p>";
    }

create_account函数内......

if ((!substr_count($response, "200 Account created successfully") > 0)) // If failed
{
    if ((substr_count($response, "400 EmailAddressInUse") > 0)) // If email address already in use
        {
            throw new emailInUseException($response);
        }
        throw new exception("Error: Could not create account. Reason: $response");
}

emailInUse捕获似乎不起作用:(

更新:启用调试后,我收到以下错误:Fatal error: Class 'emailInUseException' not found

我确信这是非常明显的。谢谢你的帮助。

1 个答案:

答案 0 :(得分:8)

    catch(emailInUse $e)
    {
        echo "<p>Error: Email already in use. Could not create account.</p>";
    }
    catch(exception $e)
    {
        echo "<p>$e</p>";           
    }

您需要更改订单。更常见的异常需要到底,否则您的代码将捕获它并在捕获特定异常之前抛出它。