gpt4 book ai didi

php - InvalidArgumentException 与 UnexpectedValueException

转载 作者:IT王子 更新时间:2023-10-29 00:58:05 27 4
gpt4 key购买 nike

我应该什么时候使用 InvalidArgumentException什么时候UnexpectedValueException ?它们在我看来是一样的。

Note一个扩展了 LogicException,另一个扩展了 RuntimeException,因此在我看来,差异应该不会那么微妙。

最佳答案

仔细查看手册页上的描述:

InvalidArgumentException

Exception thrown if an argument is not of the expected type.

(描述为如果参数与预期值不匹配则抛出异常。 until mid-2014,但已更改when PHP 5.6 got introduced)

UnexpectedValueException

Exception thrown if a value does not match with a set of values. Typically this happens when a function calls another function and expects the return value to be of a certain type or value[,] not including arithmetic or buffer related errors.

由此,我们可以得出结论,InvalidArgumentException 旨在检查传递给函数的参数类型,而 UnexpectedValueException 旨在< em>验证值与有效值集,可能在函数的内部计算期间(例如,从其他函数返回的值)。

请注意,检查参数的值是一种灰色区域;可以说,由于 InvalidArgumentException 扩展了 LogicException,它应该只处理 should lead directly to a fix in your code. 的情况。由于在输入值超出范围的情况下引发异常可能是完全预期的 runtime 行为,因此这会留下 UnexpectedValueException(扩展 RuntimeException ) 作为这种情况下的唯一候选人。

关于php - InvalidArgumentException 与 UnexpectedValueException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12483667/

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