Can I enable/disable breaking on Exceptions programmatically?

后端 未结 5 997
天涯浪人
天涯浪人 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条回答
  •  梦毁少年i
    2021-02-08 03:14

    Wrap your try catch blocks in #if DEBUG

        public void Foo()
        {
            #if DEBUG
            try
            #endif
            {
                //Code goes here
            }
            #if DEBUG
            catch (Exception e)
            {
                //Execption code here
            }
            #endif
        }
    

    I like to keep the curly braces outside of the #if that way it keeps the code in the same scope if inside or outside of debug.

    If you still want the execption handeling but want more detail you can do this

            try
            {
                //code
            }
            catch (FileNotFoundException e)
            {
                //Normal Code here
                #if DEBUG
                //More Detail here
                #endif
            }
            #if DEBUG
            catch (Exception e)
            {
                //handel other exceptions here
            }
            #endif
    

提交回复
热议问题