InvalidArgumentException vs UnexpectedValueException [英] InvalidArgumentException vs UnexpectedValueException
问题描述
何时应该使用 InvalidArgumentException ,而当 UnexpectedValueException ?他们看起来和我一样。
注意,一个扩展了LogicException,另一个扩展了RuntimeException,所以差异不应该那么微妙的IMO。
InvalidArgumentException:
< blockquote>
如果参数与预期类型不匹配,则抛出异常。
(注意:type原来是手册页面上的值,这使得它更像是类型检查的验证问题,更符合UnexpectedValueException的术语。)
UnexpectedValueException:
如果值与一组值不匹配,则抛出异常。通常,当函数调用另一个函数时,会发生这种情况,并且期望返回值为某个类型或值[,],不包括与算术或缓冲区相关的错误。
< blockquote>
从这里我们可以得出结论,
InvalidArgumentException
用于传递给函数的参数,而UnexpectedValueException
用于在函数内部计算过程中产生的值(例如从其他函数返回的值)。When should I use InvalidArgumentException and when UnexpectedValueException? They look the same to me.
Note that one extends LogicException and the other one extends RuntimeException, so the difference shouldn't be so subtle IMO.
解决方案Looking closely at the descriptions on the manual pages:
InvalidArgumentException:
Exception thrown if an argument does not match with the expected type.
(Note: "type" was originally "value" on the manual page, which makes it more a matter of validation than type-checking and more closely matches the terminology of UnexpectedValueException.)
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.
From this, we can conclude that
InvalidArgumentException
is intended for arguments passed to a function, whileUnexpectedValueException
is intended for values that arise during the internal computations of a function (e.g. values returned from other functions).这篇关于InvalidArgumentException vs UnexpectedValueException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!