问题描述
Random r = new Random();int InvadorNumberA=r.Next(0,5);int randomShot = r.Next(5);列出InvadersShooting = new List();入侵者invaderA=new Invaders();var IngresserByLocationX = 来自入侵者中的入侵者SortByLocation按入侵者SortByLocation.Location.Y分组入侵者SortByLocation进入入侵者组orderby入侵者Group.Key选择入侵者组;入侵者射击 = 入侵者ByLocationX.Last().ToList();尝试{入侵者A =入侵者射击[InvadorNumberA];//不断被扔到那里.我无法捕捉到异常.. 所以我猜它被抛出到其他地方.关于如何阻止它被抛出的任何想法?}捕获(参数OutOfRangeException dd){入侵者A =入侵者射击[0];}
堆栈跟踪
" 在 System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument 参数,ExceptionResource 资源)\r\n 在 System.ThrowHelper.ThrowArgumentOutOfRangeException()\r\n 在 System.Collections.Generic.List`1.get_Item(Int32 索引)\r\n 在 WindowsFormsApplication1.Game.ReturnFire() 在 D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\SpaceInvaders\SpaceInvaders\SpaceInvadorGame\Game.cs:line 444"
目标网站
{Void ThrowArgumentOutOfRangeException(System.ExceptionArgument, System.ExceptionResource)}
更多信息:
{"索引超出范围.必须为非负且小于集合的大小.\r\n参数名称:索引"}
{"索引超出范围.必须为非负且小于集合的大小.\r\n参数名称:索引"}
我通过简单地这样做摆脱了异常
IngressersShooting = IngresserByLocationX.Last().ToList();入侵者A =入侵者射击[r.Next(0,invadersShooting.Count)];
但我仍然很好奇,异常在哪里抛出..嗯
不要这样做.
例外应该是例外.您有一切方法可以防止这种特殊情况发生,您绝对应该这样做.
invaderA = InvaderShooting[InvadorNumberA];入侵者A =入侵者射击[0];
在第一种情况下,InvadorNumberA
可以是 0 到 4 之间的任何值.检查并查看列表中是否至少有 InvadorNumberA + 1
个元素 试图从中获取一个元素.不要依赖异常来纠正你的路线.不仅如此,也许 InvadorNumberA
实际上应该被限制为 random.Next(0, list.Count)
.当列表中可能只有 1 或 2 个元素时,为什么要创建一个从 0 到 4 的数字?
Random r = new Random();
int InvadorNumberA=r.Next(0,5);
int randomShot = r.Next(5);
List<Invaders> invadersShooting = new List<Invaders>();
Invaders invaderA=new Invaders();
var invaderByLocationX = from invadersSortByLocation in invaders
group invadersSortByLocation by invadersSortByLocation.Location.Y
into invaderGroup
orderby invaderGroup.Key
select invaderGroup;
invadersShooting = invaderByLocationX.Last().ToList();
try
{
invaderA = invadersShooting[InvadorNumberA];// constantly being thrown there. i cant catch the exception.. so i guess it is being thrown somewhere else. any idea on how i stop it from being thrown?
}
catch(ArgumentOutOfRangeException dd)
{
invaderA = invadersShooting[0];
}
stack Trace
Target Site
More info:
{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}
i got rid of the exception by simply doing this
invadersShooting = invaderByLocationX.Last().ToList();
invaderA = invadersShooting[r.Next(0,invadersShooting.Count)];
but i am still curious,,on where the exception was thrown..hmmm
Don't do this.
Exceptions should be exceptional. You have every means to prevent this exceptional scenario and you absolutely should.
invaderA = invadersShooting[InvadorNumberA];
invaderA = invadersShooting[0];
In the first case, InvadorNumberA
can be anything from 0 to 4. Check and see whether the list has at least InvadorNumberA + 1
elements in it before trying to get an element from it. Do not rely upon an exception to correct your course. More than that, perhaps InvadorNumberA
should actually be constrained to random.Next(0, list.Count)
. Why create a number from 0 to 4 when there may only be 1 or 2 elements in the list?
这篇关于参数超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!