我应该把@throws 放在文档中吗?

Should i put @throws in docs?

当在我的方法中调用的另一个方法中可能会抛出此异常时,我是否应该将 @throws 放在方法文档中?

例子

class ClassOne{
    /**
     * @throws MyException
     *
     * @return void
     */
    public function aMethod()
    {
        if(true)
        {
            throw new MyException();
        }
    }
}

class ClassTwo{
    /**
     * @throws MyException           // should I document this line?
     *
     * @return void
     */
    public function myMethod()
    {
        $classOne = new ClassOne();

        $classOne->aMethod();
    }
}

使用 PHPStorm 生成 DocBlock 时,它没有看到来自 ClassOne 的异常,也没有在 ClassTwo 文档中放置 @throws 行。

如果你没有捕捉到异常,如果你想编写干净的代码,你将使用 PHP-Doc 表示法。下一个使用您的 class 的人会得到 IDE 的消息洞察力,他必须捕获异常或将异常传递给它。 对于 运行 您的代码,这无关紧要。