System.IndexOutOfRangeException

T

Tylius

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
 
J

Justin Rogers

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.
 
T

Tylius

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!

Justin Rogers said:
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.


Tylius said:
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
 
J

Jon Skeet [C# MVP]

Tylius said:
This one line is causing the issue, I've searched all over the net, but I
can't seem to figure out why

<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.
 
T

Tylius

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?

Jon Skeet said:
Tylius said:
This one line is causing the issue, I've searched all over the net, but I
can't seem to figure out why

<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.
 
T

Tylius

That should be:

if(args.Length == 2)

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

Tylius said:
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!

Justin Rogers said:
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.
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
 
J

Jon Skeet [C# MVP]

Tylius said:
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?

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?
 
T

Tylius

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
 
M

Michael Petrotta

Tylius said:
Jon Skeet said:
Tylius said:
This one line is causing the issue, I've searched all over the net, but I
can't seem to figure out why

<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.

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?

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,
you'll get a piece of garbage returned from an invalid array access.)
 
A

Aaryn

Thanks a lot for the information, helps me understand the whole process
better

----- Original Message -----
From: "Michael Petrotta" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Sunday, January 18, 2004 3:51 PM
Subject: Re: System.IndexOutOfRangeException

"Tylius" <[email protected]> wrote in message
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?

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,
you'll get a piece of garbage returned from an invalid array access.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top