gpt4 book ai didi

php - 我应该将 PHPDoc 中的 @throws 添加到使用抛出异常的函数的函数中吗?

转载 作者:可可西里 更新时间:2023-11-01 12:37:35 25 4
gpt4 key购买 nike

例如考虑以下代码:

/**
* @param array $array
* @param string $key
* @return mixed
* @throws \InvalidArgumentException
*/
private function getArrayEntry(& $array, $key)
{
if (!array_key_exists($key, $array)) {
throw new \InvalidArgumentException(
'Invalid array of values for location. Missing '.$key.'.'
);
}

return $array[$key];
}

/**
* @param array $data
* @return Location
*/
public function createFromArray(array $data)
{
$this->getArrayEntry($data, 'name');
}

第二种方法是否也应该在 doc bloc 中包含@throws?

与有 'throws' 关键字的 Java 相比如何使用它?

最佳答案

@throws 应该只放在抛出异常的方法的docBlock 中。如果你把它放在堆栈上,它将是多余的,并且会违反 DRY。原则!

在 Java 中,您可以在 @throws 和 @exception 之间进行选择。请参阅 here

顺便说一句:您抛出了错误类型的异常。你应该抛出 \OutOfBoundsException .否则违反POLA . \InvalidArgumentException用于意外参数类型。

关于php - 我应该将 PHPDoc 中的 @throws 添加到使用抛出异常的函数的函数中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com