【问题标题】:Php, exception, is there a path which not covered?php,异常,有没有被覆盖的路径?
【发布时间】:2016-05-18 03:17:18
【问题描述】:

这个例子很难描述?

public function x()
{
    try
    {
        operation();
        return true;
    }
    catch (\Exception $e)
    {
        return false;
    }

    can this reach this line???
}

那么有没有一种既不返回true也不返回false的方法?不知何故。无论如何。

【问题讨论】:

  • 不,您无法到达该行,因为您在 try / catch 语句中返回 true 或 false。你想做什么?
  • 如果你想到达那条线,不要在try catch返回值。
  • 你不想要什么?
  • 我加了解释

标签: php exception


【解决方案1】:

您应该无法到达指定的线路。

您遇到过这种情况吗?如果是这样,请提供一些代码,我们可以告诉您为什么/如何发生。如果没有,那么你的答案是否定的。

【讨论】:

    【解决方案2】:

    解释:我只是害怕可能有某种副作用,或者什么。想象那一行:

    function a()
    {
        if (rand(0,1))
        {
            return true;
        }
        else
        {
            return false;
        }
        *************
    }
    

    检查星星。我知道他们 100% 无法到达 - 他们仍然在那里,理论上结构允许:))

    如果我这样重写:

    function a()
    {
        if (rand(0,1))
        {
            return true;
        }
        return false;
    }
    

    那么它的 101% 我确定它不会运行任何未覆盖的块

    【讨论】:

    • if (true) {return true;} 将始终被执行,所以你写什么真的没关系。这部分是什么意思:“我确定它不会运行任何未覆盖的块”?什么不会运行哪个块?
    猜你喜欢
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2011-07-03
    • 2013-03-25
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多