System.IndexOutOfRangeException [英] System.IndexOutOfRangeException

查看:60
本文介绍了System.IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一行导致了这个问题,我已经在网上搜索过了,但是我好了b $ b似乎无法弄明白为什么


public static void Main(string [] args)

{

Console.WriteLine(" Dice Roller");

try

{

int num_dice = int.Parse(args [0]); //这是问题


这是我得到的错误:


System.IndexOutOfRangeException:索引超出了数组的范围。

实际上当我使用2个参数运行可执行文件时,这是有效的,但是我很好奇为什么当我没有输入任何内容时它为什么会崩溃,因为我将
放在那里的异常处理代码,我必须做点什么

错了,这里是代码:


公共类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);

}赶上(System.FormatException)

{

Console.WriteLine(请输入两个数字参数);

Console.WriteLine("用法:<骰子数量> <边数>");

} catch(例外e)

{

Console.WriteLine(&发生,现在显示内容...... \ n");

Console.WriteLine(e);

}

}


任何人对我有什么想法?我还是C#的新手,我猜这是

我只是一个简单的错误,任何帮助都会非常感激

解决方案

首先,你应该修改你的try ... catch来捕获Exception而不仅仅是

格式异常如果

你想要它捕获IndexOutOfRangeException。其次,你永远不应该盲目地尝试args

数组并抛出异常。您只需检查args.Length即可确保

存在参数

。例外是昂贵的。

-

贾斯汀罗杰斯

DigiTec网络顾问有限责任公司。

Tylius < TY **** @ bogus.com>在消息中写道

新闻:Qk ******************* @ news04.bloor.is.net.cab le.rogers.com .. 。

这一行导致了这个问题,我已经在网上搜索了,但是我似乎无法弄明白为什么

public static void Main(string [] args)
{
Console.WriteLine(" Dice Roller);
尝试
{int / num_dice = int.Parse (参数[0]); //这是问题

这是我得到的错误:

System.IndexOutOfRangeException:索引超出了数组的范围。

实际上,当我使用2个参数运行可执行文件时,这是有效的,但我很好奇为什么当我没有输入任何内容时它会崩溃,因为我放置了异常处理那里的代码,我一定做错了什么,这里的代码是:

公共类DiceRoller
{public static void Main(string [] args)
{
Console.WriteLine(" Dice Roller");
尝试
{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);
控制台。 WriteLine(" Total Roll:{0}",RandomNumber(num_dice,num_sides));
RandomNumber(num_dice,num_sides);
} catch(System.FormatException)
{
Console.WriteLine("请输入两个数字arg uments");
Console.WriteLine(" Usage:< Dice of Number> <边数>");
} catch(例外e)
{/> Console.WriteLine(发生错误,现在显示内容...... \ n );
Console.WriteLine(e);
}
}

任何人对我有什么想法?我还是C#的新手,我猜这只是我的一个简单错误,任何帮助都会非常感激



嗯,我不确定你的意思是捕捉异常(我想我确实设置了它)b $ b所以它会抓住

什么,只输出发生的事情 - >捕获(例外e)将是

那对吗?


我之前有args.Length检查,但我以为它只会得到程序名后输入的长度

(我认为来自C ++

strlen()),我在< try>

之前放了

if(args.Length> = 2)

{


并在两次捕获后放置:


}

其他

{

Console.WriteLine(请输入两个数字参数);

Console.WriteLine(" Usage:< Dice> ;<双方数量>");

}


现在似乎工作正常,我现在以正确的方式做到了吗?我想b / b
不想进入

任何糟糕的编程习惯


感谢您的帮助!


" Justin Rogers" <菊**** @ games4dotnet.com>在消息中写道

新闻:OQ ************** @ TK2MSFTNGP10.phx.gbl ...

首先,你应该修改你的try ... catch来捕获Exception而不是
只是FormatException如果你希望它捕获一个IndexOutOfRangeException。其次,你应该
从不盲目地尝试args
数组并抛出异常。你应该简单地检查args.Length到
确保那里有一个参数
。例外是昂贵的。

-
Justin Rogers
DigiTec Web Consultants,LLC。

" Tylius" < TY **** @ bogus.com>在消息中写道
新闻:Qk ******************* @ news04.bloor.is.net.cab le.rogers.com ...

这一行引起了问题,我已经在网上搜索了,但是
我似乎无法弄明白为什么

public static void Main (string [] args)
{
Console.WriteLine(" Dice Roller);
尝试
{int / num_dice = int.Parse(args [0 ]); //这是问题

这是我得到的错误:

System.IndexOutOfRangeException:索引超出
数组的范围。

实际上当我运行带有2个参数的可执行文件时,这是有效的,但我很好奇为什么当我没有输入任何内容时它会崩溃,因为我放置了
那里的异常处理代码,我一定是做错了什么,这里的代码是:

公共类DiceRoller
{
public static void Main (string [] args)
{
Console.WriteLine(" Dice Roller);
尝试
{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("请输入两个数字c arguments");
Console.WriteLine(" Usage:< Dice of Number> <边数>");
} catch(例外e)
{/> Console.WriteLine(发生错误,现在显示内容...... \ n );
Console.WriteLine(e);
}
}

任何人对我有什么想法?我还是C#的新手,我猜这个
只是我的一个简单的错误,任何帮助都将不胜感激




Tylius< ty **** @ bogus.com>写道:

这一行引起了问题,我已经在网上搜索过,但我似乎无法弄明白为什么




< snip>


当我运行该代码时,它完全符合我的预期:它打印

out $ / $

骰子滚轴

发生错误,现在显示内容...


System.IndexOutOfRangeException:索引超出了

数组的范围。

在Test.Main(String [] args)


这是什么当发生异常时你告诉它。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


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

解决方案

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" <ty****@bogus.com> wrote in message
news:Qk*******************@news04.bloor.is.net.cab le.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



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" <Ju****@games4dotnet.com> wrote in message
news:OQ**************@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" <ty****@bogus.com> wrote in message
news:Qk*******************@news04.bloor.is.net.cab le.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




Tylius <ty****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于System.IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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