Can I enable/disable breaking on Exceptions programmatically?

后端 未结 5 1109
天涯浪人
天涯浪人 2021-02-08 02:29

I want to be able to break on Exceptions when debugging... like in Visual Studio 2008\'s Menu Debug/Exception Dialog, except my program has many valid exceptions before I get to

5条回答
  •  青春惊慌失措
    2021-02-08 03:02

    You could also use asserts instead of breakpoints. For instance, if you only want to breakpoint on the 5th iteration of a loop on the second time you call that function, you could do:

    bool breakLoop = false;
    
    ...
        Work(); // Will not break on 5th iteration.
        breakLoop = true;
        Work(); // Will break on 5th iteration.
    ...
    
    public void Work() {
        for(int i=0 ; i < 10 ; i++) {
            Debug.Assert (!(breakLoop && i == 5));
            ...
        }
    }
    

    So in the first call to Work, while breakLoop is false, the loop will run through without asserting, the second time through the loop will break.

提交回复
热议问题