1

I set my Visual Studio Exception Settings to Break When 'Common Language Runtime Exceptions' are thrown.

I have numerous routines where I catch exceptions, handle them and continue. When I'm debugging my program, I trust that these exceptions have been correctly handled and do not want the debugger to stop on them.

How can I prevent the debugger from stopping on handled exceptions? (Note, I want to break on all other CLR exceptions)

I thought DebuggerStepThrough would do the trick. However it doesn't. The following code stops on 'Method1();'

using System;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            Method1();
        }

        [System.Diagnostics.DebuggerStepThrough]
        static void Method1()
        {
            try
            {
                throw new InvalidOperationException();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

-- Edit -- Kirk - I only throw exceptions in exceptional situations. I work with Visual Studio Tools for Office. This MS library often throws exceptions that can be handled and ignored.

I would like to handle certain exceptions thrown by another library but prevent the VS2017 debugger from stopping when these errors occur.

To be clear, these exceptions are usually COM Exceptions. I don't want the debugger to ignore ALL COM Exceptions. I only want the debugger to ignore COM Exceptions that I have caught and handled.

Is this possible?

Matt Fitzmaurice
  • 1,093
  • 4
  • 13
  • 32
  • You can use `Console.Writeline(Exception)` for the debugger not to stop and ignore the exceptions thrown. – Alexis Villar Jan 30 '19 at 05:33
  • Hi Alexis. I don't think you understand the issue. I've updated the code sample to include your feedback. My problem is that the code stops on the 'Method1();' line. – Matt Fitzmaurice Jan 30 '19 at 05:41
  • Why are you using exceptions for non-exceptional situations? i.e. you should never use `try { int.Parse("a"); } catch {}` instead of (`if (!int.TryParse("a", out var i)) return false;` https://stackoverflow.com/questions/729379/why-not-use-exceptions-as-regular-flow-of-control – Kirk Woll Jan 30 '19 at 05:48
  • Under Exception Settings you can specify on which exceptions to break. If I want to just debug specific function I turn them all off, if you want to debug certain exception from somewhere you can either: 1) keep them on and as you step through the program and the exceptions pops up you can either disable it, or tell the debugger not to break on exceptions from certain library (VS2017, not sure about other VS versions). Or 2) you can just attach the debugger just before the problem occurs and continue for each unwanted exception. – CrudaLilium Jan 30 '19 at 05:51
  • Possible duplicate of [VS 17 breaking on all exceptions](https://stackoverflow.com/questions/44790784/vs-17-breaking-on-all-exceptions) – shingo Jan 30 '19 at 05:54
  • With current code in question, it will not stop on `Method1();` if you don't have any breakpoints (VS2017). – SᴇM Jan 30 '19 at 05:58
  • See if this can help you out here; [DebuggerStepThrough Attribute - How to also skip child Methods](https://stackoverflow.com/a/53990683/7444103). The debugger won't stop using the attribute describe there. The exception can be seen in the Output Window. – Jimi Jan 30 '19 at 06:04
  • SeM & Jimi - Have you tried the running the code in VS2017 with Ctrl+Alt+E "Common Language Runtime Exceptions" ticked. The code does stop on Method1(); for me. Adding the extra attribute doesn't change this behaviour. – Matt Fitzmaurice Jan 30 '19 at 07:08

1 Answers1

0

The debugger can break execution at the point where an exception is thrown, so you may examine the exception before a handler is invoked. In the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. There you can change the behaviour.

Shintaro
  • 47
  • 6