参数超出范围异常 [英] ArgumentOutofRangeException

查看:30
本文介绍了参数超出范围异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

" at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)\r\n at System.ThrowHelper.ThrowArgumentOutOfRangeException()\r\n at System.Collections.Generic.List`1.get_Item(Int32 index)\r\n at WindowsFormsApplication1.Game.ReturnFire() in D:\Documents and Settings\Dima\My Documents\Visual Studio 2008\Projects\SpaceInvaders\SpaceInvaders\SpaceInvadorGame\Game.cs:line 444"

Target Site

{Void ThrowArgumentOutOfRangeException(System.ExceptionArgument, System.ExceptionResource)}

More info:

{"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

{"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?

这篇关于参数超出范围异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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