OutOfRangeException vs. OutOfBoundsException [英] OutOfRangeException vs. OutOfBoundsException

查看:147
本文介绍了OutOfRangeException vs. OutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP为无效键定义了两个 SPL 例外:

PHP defines two SPL exceptions for invalid keys:


OutOfRangeException :请求非法索引时抛出异常。这表示在编译时应该检测到的错误。

OutOfRangeException: Exception thrown when an illegal index was requested. This represents errors that should be detected at compile time.

OutOfBoundsException :如果值不是有效的密钥,则抛出异常。这表示在编译时无法检测的错误。

OutOfBoundsException: Exception thrown if a value is not a valid key. This represents errors that cannot be detected at compile time.

由于PHP不是编译语言,编译时和运行之间的区别时间似乎很奇怪,因此我发现很难理解使用什么异常。

As PHP isn't a compiled language the distinction between compile-time and run-time seems strange and thus I am finding it hard to understand which exception to use when.

目前我的理解是应该抛出...

... OutOfRangeException 如果密钥基本上和内在的格式错误,例如如果一个数组作为一个关键字传递。

... OutOfBoundsException 如果该键通常可以,但不在某些边界,例如如果 100 被传递,但 50 是最大密钥。

Currently my understanding is that one should throw...
... OutOfRangeException if the key is fundamentally and inherently malformed, e.g. if an array is passed as a key.
... OutOfBoundsException if the key is generally okay, but isn't in some boundaries, e.g. if 100 is passed but 50 is the maximum key.

这个理解是否正确?

推荐答案

虽然PHP没有经典的编译时间很多静态检查)我将编译时间视为在编写代码时我做错了和运行时间为我的逻辑,输入或验证在某个时候关闭。

While PHP doesn't have a classic "compile time" (or a compiler that do a lot of static checks for that matter) I'd treat "compile time" as "rather static stuff I did wrong when writing the code" and "run time" as "my logic, input or validation was off at some point".

所以我的建议是这样对待:

So my suggestion would be to treat it like this:

编译时间/OutOfRangeException:错误总是可以在源代码中修复而不用或只有很少的逻辑。

"Compile Time" / "OutOfRangeException": The error can always be fixed in the source code without or with very little logic.


我总是从1-10开始输入数字,你放入11

I always take numbers from 1-10 and you put in 11






运行时间/OutOfBoundsException:错误是由于运行时错误使用。


"Run Time" / "OutOfBoundsException": The error is due to wrong use at runtime.


您创建了我,并告诉我从1到5的值,然后输入7.不计算

You created me and told me to take values from 1 to 5 then you put in 7. Doesn't compute


您要求不存在的索引,因为你没有把它放在那里,就像你应该

You request an index that is not there because you didn't put it there like you should






样品:



我希望SplFixedArray抛出一个 OutOfBoundsException ,因为它的大小是动态的,可以在运行时的机会,而我期望的东西像$ code> Calender :: getMonthName 抛出 OutOfRangeException 因为月数绝对修正为编译/写入时间。


Sample:

I'd expect an SplFixedArray to throw an OutOfBoundsException because it's size is dynamic and can chance at runtime while I'd expect something like a Calender::getMonthName to throw and OutOfRangeException because the number of month are definitely fixed at "compile/write" time.

说$ array是一个实现ArrayAccess的对象,你可以抛出一个 OutOfBoundsException 在这种情况下:

Say $array is an object that implements ArrayAccess you could throw an OutOfBoundsException in these circumstances:

$array['bar'];
$array[7];

由于值可以用于ArrayAccess,但对于SplFixedArray(5)。替代方案是 DomainException 或者可能 RangeException

As the values are what you could expect for ArrayAccess but it doesn't make sense in the case of an SplFixedArray(5). Alternatives would be DomainException or maybe RangeException

code> OutOfRangeException 在这些情况下:

An OutOfRangeException in these cases:

$calendar->getMonth(15);

作为一个数组或一个新类,在代码中肯定会有更大的逻辑缺陷,通常会导致从一个简单的哦,我把错误的变量错误的程序员。一个(可能更喜欢的)替代方案是 UnexpectedValueException 和旧的 InvalidArgumentException

As putting an array or a new class there is definitely some bigger logic flaw in the code that usually results from a simple "oh, i put in the wrong variable" error by a programmer. An (maybe preferable) alternative would be UnexpectedValueException and good old InvalidArgumentException.

对于以下情况:

$array[array()];
$array[new StdClass];



一些替代例外似乎更合适。



与Java世界的比较,当Java开发人员使用哪个异常并不总是适用的时候,有一个补充问题要处理。

some of the alternative exceptions seem more fitting.

Comparisons with the Java world on which exception to use when are not always applicable as Java Developers have an additions issue to deal with.

已选中/未检查的异常。许多人认为,不是运行时异常的所有东西在Java中的使用非常有限,不应该在内部使用),这些名称已经失去了一些原有的含义和意图。

Checked/Unchecked exceptions. With many people arguing that everything that isn't a runtime exception has very limited use in Java / should not be used much internally) those names have lost some of their original meaning and intent.

这篇关于OutOfRangeException vs. OutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆