How to NOT breaking on an exception?

2019-07-29 10:34发布

I've got something like this:

try
{
    instance.SometimesThrowAnUnavoidableException(); // Visual Studio pauses the execution here due to the CustomException and I want to prevent that.
}
catch (CustomException exc)
{
    // Handle an exception and go on.
}

anotherObject.AlsoThrowsCustomException(); // Here I want VS to catch the CustomException.

In another part of code I have multiple occurencies of situations where CustomException is thrown. I would like to force the Visual Studio to stop breaking on instance.SometimesThrowAnUnavoidableException() line cause it obscures the view of other places where I'm interested in breaking on CustomException.

I tried DebuggerNonUserCode but it is for a different purpose.

How to disable Visual Studio from catching particular exception only in a certain method?

5条回答
甜甜的少女心
2楼-- · 2019-07-29 11:14

You can use custom code to do this in two steps.

  1. Disable automatic breaking on the CustomException exception.
  2. Add a handler for the AppDomain.FirstChanceException event to your application. In the handler, if the actual exception is a CustomException, check the call stack to see if you actually want to break.
  3. Use the Debugger.Break(); to cause Visual Studio to stop.

Here is some example code:

private void ListenForEvents()
{
    AppDomain.CurrentDomain.FirstChanceException += HandleFirstChanceException;
}

private void HandleFirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    Exception ex = e.Exception as CustomException;
    if (ex == null)
        return;

    // option 1
    if (ex.TargetSite.Name == "SometimesThrowAnUnavoidableException")
        return;

    // option 2
    if (ex.StackTrace.Contains("SometimesThrowAnUnavoidableException"))
        return;

    // examine ex if you hit this line
    Debugger.Break();
}
查看更多
三岁会撩人
3楼-- · 2019-07-29 11:16

You can't simply disable Visual Studio from stoping in a particular place of code. You can only prevent it to stop when particular type of exception is thrown but that will affect all places where such exception occurs.

Actually you can implement custom solution as suggested by 280Z28.

查看更多
SAY GOODBYE
4楼-- · 2019-07-29 11:18

You can disable stepping altogether by placing the DebuggerStepThrough Attribute before the method. As this disables stepping in the whole method, you may isolate the try-catch into a seperate one for debugging purposes.

I did not test, but it should not even break in that method when en exception is thrown. Give it try ;-)

See also this SO thread

查看更多
可以哭但决不认输i
5楼-- · 2019-07-29 11:21

In Visual Studio, go to debug->exceptions and turn off breaking for your CustomException by unchecking the appropriate checkbox, then set a breakpoint in the code (probably on the catch statement) on the places you actually want to break on.

查看更多
孤傲高冷的网名
6楼-- · 2019-07-29 11:22

If you want Visual Studio to stop breaking on all exceptions of a type, you have to configure the behavior from the Exceptions window.

Full instructions are here, but the gist is to go to the Debug menu and choose exceptions, then uncheck the items you dont want the debugger to break on.

I don't think there is a way to avoid a specific method using this technique, but maybe the better question is "why is this throwing an exception?"

You could add a set of #IF DEBUG pre-processor instructions to avoid running the problematic sections of code.

查看更多
登录 后发表回答