ArgumentOutofRangeException

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];
       }

Trace de la pile

" au Système.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource de ressources)\r\n au Système.ThrowHelper.ThrowArgumentOutOfRangeException()\r\n au Système.Les Collections.Génériques.Liste`1.get_Item(Int32 index)\r\n à WindowsFormsApplication1.Jeu.ReturnFire() dans D:\Documents and Settings\Dima\Mes Documents\Visual Studio 2008\Projects\SpaceInvaders\SpaceInvaders\SpaceInvadorGame\Jeu.cs:ligne 444"

Site Cible

{Void ThrowArgumentOutOfRangeException(Système D'.ExceptionArgument, Système.ExceptionResource)}

Plus d'infos:

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

{"Index était hors de portée. Doit être non négatif et inférieur à la taille de la collection.\r\nParameter nom: index"}

je me suis débarrassé de l'exception simplement en faisant ce

 invadersShooting = invaderByLocationX.Last().ToList();

           invaderA = invadersShooting[r.Next(0,invadersShooting.Count)];

mais je suis toujours curieux,où l'exception a été levée..hmmm

OriginalL'auteur Dmitry Makovetskiyd | 2011-04-15