You cannot handle a stack overflow exception because one of the things the code has to do in order to record information about an exception is to call some code.
Since you have overflowed the stack, one of the things it absolutely cannot do is call more code.
Hence why Windows just terminates the program.
A StackOverflowException
is thus uncatchable, unless you explicitly throw it yourself before the actual stack actually overflow.
Now, that answers the question you asked.
The real problem here is that the code as shown does not in fact call itself, but the name of the method _SelectedIndexChanged
means that something in that code makes the checkedListBox1
call this method again.
It probably shouldn't.
You should place a breakpoint inside this method and hit F5 a couple of times and inspect the call stack to see which statement inside is causing this to happen.
There is another reason why this might happen and I can best describe it by referring to a full glass of water. If you have a glass of water that is already full (but has not yet overflowed) and you put one extra drop into the glass, and then it overflows, was it that one single drops fault, or was it all the billion of other drops that were put into the glass before it?
There might be something completely different that is causing the stack overflow to happen, it just so happens this method is the very last drop that overflowed the stack.
Try the breakpoint, see how far you get.