4

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.
Charlie
  • 4,827
  • 2
  • 31
  • 55
  • Why don't you put the line tempcollection[0].Groups["data"].Value inside the if statement "if (tempcollection.Count != 0)"? If the count is 0, "string ThisDiv = tempcollection[0].Groups["data"].Value;" will throw "Out of Range" exception. – Sormita Chakraborty Mar 27 '15 at 06:06
  • @SormitaChakraborty It's the call to Count itself that's throwing the exception. I'm having the same problem. – João Mendes Jul 19 '19 at 15:44

1 Answers1

2

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.

Neeraj
  • 596
  • 7
  • 9
  • let me check that ,Neeraj is it unchecked exception ,why it is not caught – Charlie Mar 27 '15 at 06:18
  • Java has a concept of defining what a method might through not just through comments but is part of code semantics. .Net in turn does not have this semantic, but instead all such information is part of such methods documentation. So for an 'int.Parse' (https://msdn.microsoft.com/en-us/library/b3h1hf19(v=vs.110).aspx) methods code documentation defines that it can have these exceptions. You will have to explicitly write your code based on this documentation to handle any expected exceptions. Somewhat like below: try { // regex logic } catch (IndexOutOfRangeException iore) { // other logic } – Neeraj Mar 27 '15 at 06:38
  • @Neeraj It's the call to Count itself that's throwing the exception. I'm having the same problem. – João Mendes Jul 19 '19 at 15:45
  • You are getting an index out of range at count ? If you dont mind a snippet would help. – Neeraj Jul 23 '19 at 02:43