Groups | Blog | Home
all groups > c# > january 2004 >

c# : System.IndexOutOfRangeException


Justin Rogers
1/17/2004 3:09:10 PM
First off, you should modify your try...catch to catch Exception instead of just
FormatException if
you want it to catch an IndexOutOfRangeException. Second, you should never
blindly try the args
array and throw an exception. You should simply check the args.Length to make
sure there is a parameter
there. Exceptions are costly.


--
Justin Rogers
DigiTec Web Consultants, LLC.


[quoted text, click to view]

Tylius
1/17/2004 10:05:36 PM
This one line is causing the issue, I've searched all over the net, but I
can't seem to figure out why

public static void Main(string[] args)
{
Console.WriteLine("Dice Roller");
try
{
int num_dice = int.Parse(args[0]); // This is the problem

this is the error I get:

System.IndexOutOfRangeException: Index was outside the bounds of the array.


Actually this works when I run the executable with 2 arguments, but I'm
curious as to why it's crashing when I don't input anything, because I
placed the exception-handling code in there, I must be doing something
wrong, here's the code:

public class DiceRoller
{
public static void Main(string[] args)
{
Console.WriteLine("Dice Roller");
try
{
int num_dice = int.Parse(args[0]);
int num_sides = int.Parse(args[1]);
Console.WriteLine("Now rolling: {0}d{1}", num_dice, num_sides);
Console.WriteLine("Total Roll: {0}", RandomNumber(num_dice, num_sides));
RandomNumber(num_dice, num_sides);
} catch (System.FormatException)
{
Console.WriteLine("Please enter two numeric arguments");
Console.WriteLine("Usage: <Number of Dice> <Number of Sides>");
} catch (Exception e)
{
Console.WriteLine("An error has occured, displaying contents now...\n");
Console.WriteLine(e);
}
}

Anyone have any ideas for me? I'm still new to C#, and I'm guessing this is
just a simple mistake on my part, any help would be greatly appreciated

Tylius
1/18/2004 5:58:17 AM
Hmm, I'm not sure what you mean by catch Exception (I think I did set it up
so it would catch
anything and just output what happened -> the catch (Exception e) would be
that right?

I had the args.Length check in there before, but I was thinking it would
just be getting the length
of whatever was inputted after the program name (I thought that from the C++
strlen() ), I placed
this before the <try>

if(args.Length >= 2)
{

And placed this after the two catches:

}
else
{
Console.WriteLine("Please enter two numeric arguments");
Console.WriteLine("Usage: <Number of Dice> <Number of Sides>");
}

It seems to work properly now, have I done this the proper way now though? I
don't want to get into
any bad programming practices

Thanks for the help!

[quoted text, click to view]

Jon Skeet [C# MVP]
1/18/2004 7:55:25 AM
[quoted text, click to view]

<snip>

When I run that code, it does exactly what I expect it to: it prints
out

Dice Roller
An error has occured, displaying contents now...

System.IndexOutOfRangeException: Index was outside the bounds of the
array.
at Test.Main(String[] args)

which is what you told it to do when an exception occurs.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
michael_petrotta NO[at]SPAM yahoo.com
1/18/2004 12:51:02 PM
[quoted text, click to view]

The array holds all command-line arguments to your application; when
you executed your application without arguments, the array was empty.
Then you attempted to access the first element in the array (args[0]).
Well, there *is* no first element, so you got your exception.

(IndexOutOfRangeException: the index you passed, 0, was out of the
range of allowable indexes for that array. In this case, because the
array was empty, there were no allowable indexes. Makes you
appreciate a checked environment like the CLR; in languages like C,
Tylius
1/18/2004 2:10:19 PM
I was trying to figure out why it was throwing the exception though. Is it
because the array would be empty by default, so when it's trying to parse,
there's nothing to parse or something?

[quoted text, click to view]

Tylius
1/18/2004 2:11:37 PM
That should be:

if(args.Length == 2)

not args.length >= 2, I don't want more than two arguments also

[quoted text, click to view]

Jon Skeet [C# MVP]
1/18/2004 7:13:13 PM
[quoted text, click to view]

It's throwing the exception because you're trying to get at the first
item in a list with *no* items. Put it this way: if you didn't expect
an exception, what did you expect to happen? What did you expect to get
passed to int.Parse?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
Tylius
1/18/2004 8:18:40 PM
Good point, I never even though about that, I turned the whole thing into a
GUI and with the exception handling / input checks, everything's working
perfectly, thanks for the help

[quoted text, click to view]

Aaryn
1/18/2004 10:16:48 PM
Thanks a lot for the information, helps me understand the whole process
better

----- Original Message -----
From: "Michael Petrotta" <michael_petrotta@yahoo.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Sunday, January 18, 2004 3:51 PM
Subject: Re: System.IndexOutOfRangeException


[quoted text, click to view]

AddThis Social Bookmark Button