0

I am trying to catch an unhandled exception but it still occurs an error. Where do I need to start my "try" and when/how do I need to catch it ? Here is my code:

            String allMarks = "";
            String toString1 = "";
            String toString2 = "";

            if (eventcounter.Count > 2)
            {
                allMarks = allMarks + eventcounter[0];

                for (int i = 0; i < eventcounter.Count; i++)
                {
                    toString1 = eventcounter[i].ToString();

                    for (int j = 1; j <= eventcounter.Count; j++)
                    {
                        try
                        {
                            toString2 = eventcounter[j].ToString();
                        }
                        catch (System.IndexOutOfRangeException)
                        {
                            Console.WriteLine("not enough elements to compare");
                            throw new System.ArgumentOutOfRangeException("Index parameter is out of range");
                        }

                        if (toString1 != toString2)//(eventcounter[i] != eventcounter[j])
                        {
                            allMarks = allMarks + eventcounter[j];
                            Console.WriteLine(allMarks);
                            //_sensorTextView3.Text = string.Format("Eventcounter: {0}", allMarks);
                        }
                    }
                }
               // _sensorTextView3.Text += allMarks;
            }

UPDATE: Okay I am using only one loop now instead of two but still the error occurs whenever run the code

for (int i = 0; i < eventcounter.Count; i++)
                {
                    try
                    {
                        toString1 = eventcounter[i].ToString();
                        toString2 = eventcounter[i + 1].ToString();
                    }
                    catch (System.IndexOutOfRangeException)
                    {

                        throw new System.ArgumentOutOfRangeException("Index parameter is out of range");
                    }

                    if (toString1 != toString2)
                    {
                        allMarks = allMarks + eventcounter[i+1];
                        Console.WriteLine(allMarks);
                    }
mx1txm
  • 139
  • 2
  • 10
  • 1
    `for (int j = 1; j <= eventcounter.Count; j++)` should probably be `for (int j = 1; j < eventcounter.Count; j++)` and the exception probably occurs in `allMarks = allMarks + eventcounter[j];` (outside of the try/catch block) – vc 74 Apr 04 '19 at 07:58
  • You can put your `try` block before your `if` statement and catch any corresponding exceptions related to your indexes or conversions. – Rahul Sharma Apr 04 '19 at 07:58
  • 2
    `IndexOutOfRangeException` should never be caught; it's what Eric Lippert calls a [bone-headed exception](https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/), in that your code shouldn't be causing it in the first place. Arrays are indexed from 0 and have valid indexes from 0 to their length, *exclusive*; change your `<= Count` checks to `< Count`. Better yet, see if you can do this without nested loops -- do you really need to compare every element to every other element, repeatedly, or could this be done in one loop? (Hint: it can.) – Jeroen Mostert Apr 04 '19 at 07:59
  • 1
    Instead of **handling** an exception you should allways try to **avoid** it in the first place. In particular in a case of a `NullReference-`, `Argument-` or `IndexOutOfRangeException` - to name only a few. Anyway: where *does* your exception occur? Obviously it´s not within the `eventCouinter[j].ToString`-part. – MakePeaceGreatAgain Apr 04 '19 at 08:03
  • What is the *initial problem* you are trying to solve, please? – Dmitry Bychenko Apr 04 '19 at 08:08
  • @JeroenMostert oh yes you are right, I have changed it to just one loop :) thanks – mx1txm Apr 04 '19 at 08:34
  • I did but still get the error. Does it matter where to put the catch argument? I did it in the very end – mx1txm Apr 04 '19 at 08:39
  • I am saving all events form my Sensor data in an eventcounter. But I only want to save and also display when the Value changes so I go through each two eleemnts of my event counter to compare if the value has changed and if yes to save it in allMarks as a String – mx1txm Apr 04 '19 at 08:46

1 Answers1

0

eventcounter contains eventcounter.Count elements, numbered from 0 to eventcounter.Count-1.

with

for (int j = 1; j <= eventcounter.Count; j++)

at some point j will become equal to eventcounter.Count. Since the element number range is [0, eventcounter.Count], an attempt to access element number eventcounter.Count throws IndexOutOfRangeException.

rs232
  • 1,248
  • 8
  • 16