1
votes

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?

1
You can use Console.Writeline(Exception) for the debugger not to stop and ignore the exceptions thrown. - Alexis Villar
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
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; stackoverflow.com/questions/729379/… - Kirk Woll
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
Possible duplicate of VS 17 breaking on all exceptions - shingo

1 Answers

0
votes

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.