A first chance exception of type 'System.IndexOutOfRangeException' in exe in output window

1.4k Views Asked by At

I am running a Windows form program that does all its work on another thread. After some time, the UI freezes and stops responding. The background thread is still working fine (I can see that from the work done).

I got this exception:

A first chance exception of type 'System.IndexOutOfRangeException' in exe

When I traced the line of exception and ran it again, I got this:

The thread <No Name> (0x19b4) has exited with code 0 (0x0).

The line of code runs fine but it gives a System.IndexOutofRangeException.

MatchCollection tempcollection = Regex.Matches(document,
        "(?<data>More information from(.|\\r|\\n)*?</div>)");
if (tempcollection.Count == 0)
{
    return Result;
}
string ThisDiv = tempcollection[0].Groups["data"].Value;
// The above line shows exception in Output Window,
// otherwise it works fine and moves to next line.

UPDATE: I've seen that output gives information about every exception whether it is caught or not ,I thought that is cause of freezing UI but that is not.

  1. Kindly help me to get rid of this exception.
  2. Is it an Unchecked exception; I learned in my studies that .Net does not have unchecked exceptions. Please clarify this for my understanding.
1

There are 1 best solutions below

4
Neeraj On

The regex might not have returned a group value and tempcollection[0].Groups["data"] might not be set hence the indexer on group would fail and you could get an index out of range exception try adding a null check on the same before try to get a value from it.

Try verifying the regex on the input data to validate that the above is true on any regex tool.