Ryszard Dżegan
Ryszard Dżegan

Reputation: 25414

How to NOT breaking on an exception?

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?

Upvotes: 1

Views: 1587

Answers (5)

Ryszard Dżegan
Ryszard Dżegan

Reputation: 25414

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.

Upvotes: 0

Sam Harwell
Sam Harwell

Reputation: 99859

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();
}

Upvotes: 2

DasKrümelmonster
DasKrümelmonster

Reputation: 6060

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

Upvotes: 0

David S.
David S.

Reputation: 6105

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.

Upvotes: 1

StingyJack
StingyJack

Reputation: 19459

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.

Upvotes: 0

Related Questions