exception handling in c#
My project about networking.It gives IndexOutOfBound exception. I write try,catch block.This exception is handled but press any key to continue is message is display. give any开发者_如何学运维 solution.
You need to post code, to get a really accurate answer. However, I'll try to help you understand what's going on.
I am guessing that you actually meant you were receiving an IndexOutOfRangeException
, which comes with a message, "Index was outside the bounds of the array". This means that you are trying to access an object in an array at an index, that is outside the bounds of the array. So, if there are 2 items in an array and you try to access the third item, then you will get this exception. New programmers get this error a lot, because they forget that arrays start at an index of 0 and not 1, so if you want to get the first item in an array you would access it like yourArrayInstance[0]
.
Below is a simple program that causes the same exception. Try to find something in your code that looks like it could do a similar action, and you just might find the source of your error.
void Main()
{
var ary = new string[]{"hi","what's up"};
Console.WriteLine(ary[2]);
}
However, you don't want to swallow the exception. If this exception happens, it means you are doing something wrong and you need to fix it.
精彩评论