0

I am running a Console C#.net program in Debug mode in Visual Studio 2022 (Community Edition). It has the following loop:

for (int i = 0; i < result.Length - 1; ++i)
{
    result[i] = row[i - 1] + row[i];
}

Of course, there is an out of bound exception at the first iteration. I have set the exception settings like this:

enter image description here

Nonetheless, when the exception happens, the following is the output in the Console and the debug mode is not entered:

6
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at ConsoleApp2.Program.next_row(Int32[] row) in C:\Users\mgold\source\repos\ConsoleApp2\Program.cs:line 11
   at ConsoleApp2.Program.Main(String[] args) in C:\Users\mgold\source\repos\ConsoleApp2\Program.cs:line 29

C:\Users\mgold\source\repos\ConsoleApp2\bin\Debug\net6.0\ConsoleApp2.exe (process 15220) exited with code -532462766.
Press any key to close this window . . .

Interestingly, this does not happen for all the runs, but I cannot figure out what it depends on. What could be the problem?

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • Does it only happen in VS2022? Have you tried using VS2019? As much as I love its hot-reload and other new features, it is nowhere as stable as VS2019. – thedemons Oct 23 '22 at 08:23
  • @thedemons I did not have this happen to me in the earlier versions (not sure whether I had 2017 or 2019 on my previous laptop)... – AlwaysLearning Oct 23 '22 at 08:42
  • When a form is constructed often events occur before all the control are created. So in the event handlers you have to add code to prevent the code in the event from running. For example a DGV the number of rows is null before the control constructed and the number of rows is -1 before columns are added. So in event you have to return if rows is null or -1. – jdweng Oct 23 '22 at 08:53
  • @jdweng What form are you referring to? It is a console application (stated at the beginning of the question). – AlwaysLearning Oct 23 '22 at 09:02
  • @AlwaysLearning : The exception has : ConsoleApp2.Program.next_row. Where is the row? – jdweng Oct 23 '22 at 09:16
  • @jdweng It is a variable of type int[]. – AlwaysLearning Oct 23 '22 at 09:36
  • Can you post the whole code. I'm guessing what you have posted is not the cause. – Neil Oct 23 '22 at 09:36
  • Most likely because of https://stackoverflow.com/questions/2468852/the-breakpoint-will-not-currently-be-hit-the-source-code-is-different-from-the – Rand Random Oct 23 '22 at 10:13
  • Put a break point on line and check value. – jdweng Oct 23 '22 at 11:36

0 Answers0