1

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?

Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56
  • What version of Visual Studio? In 2012 you'd just untick "Break on this type of exception" – PhonicUK Apr 05 '13 at 12:06
  • 1
    AFAIK this is not possible - you can turn breaking on and off for a particular exception type, but not for a particular section of code. – Nick Butler Apr 05 '13 at 12:09
  • Why is this exception "unavoidable"? – StingyJack Apr 05 '13 at 12:18
  • 1
    Do not use exceptions for flow control, you regret them when you have to debug them. Take the DateTime.TryParse() method as an example of a method that was designed to avoid exceptions in places where they are likely to occur. – Hans Passant Apr 05 '13 at 12:30
  • @StingyJack: It is unavoidable because I've got a class, that takes a string as a constructor argument in order to parse it. I don't have any members to validate such string which is unknown at design time. It is a third party class. If the input string is invalid it throws an argument exception. – Ryszard Dżegan Apr 05 '13 at 12:37
  • @Hans Passant: What would you do if you were given a class that takes a string as a constructor parameter and didn't have any members of that class to validate that input argument? – Ryszard Dżegan Apr 05 '13 at 12:43
  • 2
    I would validate it in another class. – John Saunders Apr 05 '13 at 13:09
  • 2
    I would talk to the programmer of that class. Instead of some random guy on the Internet. – Hans Passant Apr 05 '13 at 13:10
  • Based on the Exception details, can you tell the difference between a "legitimate" exception throw that you wish to continue execution, and a "illegitimate" exception throw where you wish to stop execution? – StingyJack Apr 08 '13 at 16:52
  • @StingyJack: Let us assume that some properties are different (Message, InnerException etc.) in both exceptions of same type. How to instruct VS to get through one of them basing on that? – Ryszard Dżegan Apr 09 '13 at 11:52

5 Answers5

2

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();
}
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
1

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.

David S.
  • 5,965
  • 2
  • 40
  • 77
  • I'd prefer stack walking after the exception, but of course you are right. – Ryszard Dżegan Apr 05 '13 at 12:22
  • @yBee you can still view the call stack in the call stack window, if that's what you mean. – David S. Apr 05 '13 at 12:30
  • In order to view the call stack I have to at first break in meaningfull place. The Visual Studio is breaking often in inappropriate place what is interfering with the place, that I want to stop. – Ryszard Dżegan Apr 05 '13 at 12:46
0

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.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • In the line, where I want to avoid breaking I can't avoid that exception in some cases. That exception have to be sometimes thrown and it is valid (and it is a thirdparty code). But in other situations I want to break if such exception occured and fix the issue. The problem is, that in some state of application, the unwanted line is breaking many times and I don't want to stop and click to continue each time and I can't disable that exception type, because it will be missed in invalid place that I need to investigate. – Ryszard Dżegan Apr 05 '13 at 12:29
  • Read the last point. You can disable it while debugging, when the code is compiled in release mode it is still thrown. – StingyJack Apr 05 '13 at 13:50
  • I can't disable it. It must be running and throwing an exception but it shouldn't pause the execution. – Ryszard Dżegan Apr 08 '13 at 11:50
0

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

Community
  • 1
  • 1
DasKrümelmonster
  • 5,816
  • 1
  • 24
  • 45
0

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.

Community
  • 1
  • 1
Ryszard Dżegan
  • 24,366
  • 6
  • 38
  • 56