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" <tylius@bogus.com> wrote in message news:QkiOb.89177$%wh.88632@news04.bloor.is.net.cable.rogers.com... > 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 > >
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
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] "Justin Rogers" <Justin@games4dotnet.com> wrote in message news:OQxZ87U3DHA.2500@TK2MSFTNGP10.phx.gbl... > 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" <tylius@bogus.com> wrote in message > news:QkiOb.89177$%wh.88632@news04.bloor.is.net.cable.rogers.com... > > 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 > > > > > >
[quoted text, click to view] Tylius <tylius@bogus.com> wrote: > 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. -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
[quoted text, click to view] "Tylius" <tylius@bogus.com> wrote in message news:<ftwOb.95696$%wh.42794@news04.bloor.is.net.cable.rogers.com>... > > "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message > news:MPG.1a744c48cc32d584989eac@msnews.microsoft.com... > > Tylius <tylius@bogus.com> wrote: > > > 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,
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] "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1a744c48cc32d584989eac@msnews.microsoft.com... > Tylius <tylius@bogus.com> wrote: > > 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. > > -- > Jon Skeet - <skeet@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too
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] "Tylius" <tylius@bogus.com> wrote in message news:ZfpOb.71158$ZuL1.16096@twister01.bloor.is.net.cable.rogers.com... > 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" <Justin@games4dotnet.com> wrote in message > news:OQxZ87U3DHA.2500@TK2MSFTNGP10.phx.gbl... > > 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" <tylius@bogus.com> wrote in message > > news:QkiOb.89177$%wh.88632@news04.bloor.is.net.cable.rogers.com... > > > 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 > > > > > > > > > > > >
[quoted text, click to view] Tylius <tylius@bogus.com> wrote: > 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? -- Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet
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] "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message news:MPG.1a74eb27f73707c0989eb2@msnews.microsoft.com... > Tylius <tylius@bogus.com> wrote: > > 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? > > -- > Jon Skeet - <skeet@pobox.com> > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too
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] > "Tylius" <tylius@bogus.com> wrote in message news:<ftwOb.95696$%wh.42794@news04.bloor.is.net.cable.rogers.com>... > > > > "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message > > news:MPG.1a744c48cc32d584989eac@msnews.microsoft.com... > > > Tylius <tylius@bogus.com> wrote: <snip>> > > > > 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.)
Don't see what you're looking for? Try a search.
|