簡體   English   中英

Try-Catch 沒有捕捉到自定義異常類型

[英]Try-Catch not catching custom exception type

這是一個非常簡單的問題,顯然是我自己的新手身份阻礙了我。 道歉。

為什么這段代碼不起作用?

    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 function 內...

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 catch 似乎不起作用:(

更新:啟用調試后,出現以下錯誤: Fatal error: Class 'emailInUseException' not found

我敢肯定這是非常明顯的事情。 謝謝你的幫助。

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

您需要更改訂單。 更常見的異常需要到底,否則您的代碼將捕獲它並在捕獲特定異常之前拋出它。

捕獲所有異常


try{

}catch(Throweable $exception)
{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM