Try 块中的 C# 错误涉及循环、数组、下标和 IndexOutOfRangeException
我在下面包含了我的源代码草稿。如果您对我的错误做法有任何意见,我将不胜感激。我不确定我的语法是否正确...另外,我确实在 Cramster 上找到了一个示例;但我不确定该示例是否按照说明实现了“下标”(如果我错了,请指出)。我还认为“for”循环相当重复,因为它似乎正在建立与下标应该建立的相同的东西......此代码是为了响应以下作业:
“编写一个程序,在其中声明一个五个整数的数组并存储五个值 数组。编写一个 try 块,在其中放置一个循环来尝试访问每个 数组的元素,将下标从 0 递增到 10。创建一个 catch 块, 捕获最终的 IndexOutOfRangeException;在块内,显示“现在 你已经太过分了。”在屏幕上。将文件另存为 GoTooFar.cs。”
Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell
我的源代码有错误:
using System;
namespace Further
{
public class GoTooFar
{
public static void Main()
{
private static int[] fiveIntArray = {1, 2, 3, 4, 5};
//private static int CUTOFF = 11;
int subscript;
int rate;
try
{
//bool further;
//public static int DetermineArray(int further)
for(int x = 0; x < 10; x++)
if(further < 11)
throw new IndexOutofRangeException("Now you've gone too far.");
subscript = 0;
else
subscript = 10;
rate = fiveIntArray[subscript];
return rate;
}
catch(IndexOutOfRangeException e)
{
throw;
Console.WriteLine(e.StackTrace);
//Console.WriteLine("Now you've gone too far.");
//return e;
}
}
}
}
//我在 Cramster.com 上找到的示例:
using System;
namespace Console2
{
class Class1
{
static void Main(string[] args)
{
int[] numbers = new int[5] {1, 2, 3, 4, 5};
try
{
for(int i=0;i<10;i++)
if(i>5)
throw new IndexOutOfRangeException("Now you’ve gone too far.");
}
catch(IndexOutOfRangeException e)
{
throw;
}
}//end ma...
}
}
I have included my source code draft below. I would appreciate any input on what I'm doing incorrectly. I'm not sure my syntax is correct... Also, I did find an example on Cramster; but I'm not sure that the example implemented the "subscript" as directed (please point it out if I am wrong in this) by the instructions. I also think that the "for" loop is rather repetitive since it appears that it's establishing the same thing that the subscript is supposed to establish... This code is in response to the following assignment:
"Write a program in which you declare an array of five integers and store five values in
the array. Write a try block in which you place a loop that attempts to access each
element of the array, incrementing a subscript from 0 to 10. Create a catch block that
catches the eventual IndexOutOfRangeException; within the block, display “Now
you’ve gone too far.” on the screen. Save the file as GoTooFar.cs."
Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
My source code with errors:
using System;
namespace Further
{
public class GoTooFar
{
public static void Main()
{
private static int[] fiveIntArray = {1, 2, 3, 4, 5};
//private static int CUTOFF = 11;
int subscript;
int rate;
try
{
//bool further;
//public static int DetermineArray(int further)
for(int x = 0; x < 10; x++)
if(further < 11)
throw new IndexOutofRangeException("Now you've gone too far.");
subscript = 0;
else
subscript = 10;
rate = fiveIntArray[subscript];
return rate;
}
catch(IndexOutOfRangeException e)
{
throw;
Console.WriteLine(e.StackTrace);
//Console.WriteLine("Now you've gone too far.");
//return e;
}
}
}
}
//The example I found on Cramster.com:
using System;
namespace Console2
{
class Class1
{
static void Main(string[] args)
{
int[] numbers = new int[5] {1, 2, 3, 4, 5};
try
{
for(int i=0;i<10;i++)
if(i>5)
throw new IndexOutOfRangeException("Now you’ve gone too far.");
}
catch(IndexOutOfRangeException e)
{
throw;
}
}//end ma...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该抛出异常,只需捕获运行时抛出的异常即可。还有其他几个问题:
私有静态
即可。它可以很简单:
注意,我们实际上并不使用
rate
。它就在那里,所以它将是一个有效的声明。You're not supposed to throw an exception, just catch the one the runtime throws. There are several other issues:
private static
.It can be as simple as:
Note, we don't actually use
rate
. It's just there so it will be a valid statement.