Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
my promgram bugs after the catch and won't run anymore.

anyone know how to skip a out of index? without try; catch?

C#
try
{
    lines2 = ResultViewshown2[i].Split(new string[] { "\r\n", "\n" },    StringSplitOptions.None);

    for (int x = 0; x < 10000; x++)
    {
            string line = lines2[x];

            if (!line.Contains(check2))
            {
                Resultadd2.Add((string.Format("{0}\r\n", line ?? string.Empty)));
            }
    }
}
catch(IndexOutOfRangeException)
{
}
Posted
Comments
[no name] 14-Apr-14 9:32am    
Why aren't you checking the size of lines2?
Wessel Beulink 14-Apr-14 9:37am    
I tried but the index is always 10.000.

still getting error after size check of lines2
[no name] 14-Apr-14 9:48am    
You tried what? If the length of lines2 is always 10000 then how are you getting an Index out of range? How do you know when you are simply ignoring the exception?
Wessel Beulink 14-Apr-14 10:02am    
because the index is a range of items in a listbox,

i manual copyed 10.000 lines of text in there. i wasn't ingnoring the exeption but it crashed my program.

the IsNullOrEmpty was a good fix for it. still didn't understand my fault.
[no name] 14-Apr-14 10:12am    
Well I sure don't know either since what you just said makes no sense to me. And an empty catch block is ignoring the exception.

1 solution

I would be following more defensive approach. See below:

C#
try
{
    lines2 = ResultViewshown2[i].Split(new string[] { "\r\n", "\n" },    StringSplitOptions.None);

    for (int x = 0; x < lines2.length; x++)
    {
        string line = lines2[x];

        if(!string.IsNullOrEmpty(line))
        {
            if (!line.Contains(check2))
            {
                Resultadd2.Add((string.Format("{0}\r\n", line ?? string.Empty)));
            }
        }
    }
}
catch(IndexOutOfRangeException)
{
    //handle your exception
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Apr-14 9:40am    
Don't tell people to use empty catch blocks; there is enough bad code being generated as it is.
Manas Bhardwaj 14-Apr-14 9:51am    
Thanks.

I didn't even look at it. Fixed now!
Wessel Beulink 14-Apr-14 9:45am    
thanks! this help me out, it was an array so .length won't fit in here.

I used a LongLength.

why won't u use a empty catch block Richarc MacCutchan?
Richard MacCutchan 14-Apr-14 9:57am    
Using an empty catch block means you catch the error and then throw it away. So your program proceeds quite happily ignoring what could be a fatal error, and you will not discover it until too late. I just hope the people who are doing it that way are not writing code for my bank.
Manas Bhardwaj 14-Apr-14 9:59am    
Ditto!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900