douniang3866 2011-04-05 17:18
浏览 308
已采纳

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

This is a pretty simple question, and it's blatantly my own newb status that's holding me back. Apologies.

Why isn't this bit of code working?

    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>";
    }

Within the 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");
}

The emailInUse catch doesn't seem to be working :(

Update: With debugging enabled I get the following error: Fatal error: Class 'emailInUseException' not found

I'm sure it's something really awfully obvious. Thanks for any help.

  • 写回答

1条回答 默认 最新

  • douhu7807 2011-04-05 17:21
    关注
        catch(emailInUse $e)
        {
            echo "<p>Error: Email already in use. Could not create account.</p>";
        }
        catch(exception $e)
        {
            echo "<p>$e</p>";           
        }
    

    You need to change the order. The more general exceptions need to come to the bottom, otherwise your code will simply catch it and throw it before the specific exception is caught.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序