C#: Why is this code ignored

cwinforms

Sorry about the weird question title, but I don't really know what to call this. It simply makes no sense to me. Here is the code:

public partial class ParameterPanel : FlowLayoutPanel
{
    ...

    public void SetContents(IEnumerable<IParameter> parameters)
    {
        if (parameters == null || !parameters.Any())
            return;

        SuspendLayout();
        Controls.Clear();

        foreach (IParameter parameter in parameters)
        {
            Control control = Factory.Create(parameter);
            Controls.Add(control);
        }

        Console.WriteLine("???");

        ResumeLayout(false);
        PerformLayout();
    }
}

The weird thing is that the code sometimes never gets to the Console.WriteLine I break in the beginning of the method and try to step through it. It goes into the foreach loop, but after the last item, the method just returns?? It never reaches the Console.WriteLine. And I just don't get it… How is this even possible? And the weirdest thing is that it doesn't happen always either. But it happens consistently in the cases it does.

Anyone have a clue what is going on here? I don't even know where to start looking for this bug.

Best Answer

If something in the method is throwing an exception the rest of the method will be skipped.

Hit Debug->Exceptions and tick the box for CLR exceptions to make VS break as soon as an exception is thrown.