I have code that frequently throws an InvalidOperationException
. In this specific case, the exception is actually OK and I don't want the debugger to break when it gets thrown. However, I can't disable all InvalidOperationException
breaks because that's just a bad idea.
Supposedly the DebuggerStepThrough
or DebuggerHidden
attributes are what I need, but the exception bubbles up and ignores the try
block - the debugger breaks anyways.
internal class Program
{
static void Main(string[] args)
{
// The debugger breaks at this line.
// Ideally it should continue execution.
var x = TestFunc();
}
[System.Diagnostics.DebuggerHidden]
private static bool TestFunc()
{
try
{
// The actual code is third-party; I can't control the exception.
throw new InvalidOperationException();
}
catch (InvalidOperationException)
{
return false;
}
catch
{
throw;
}
}
}
Related issues:
How to NOT breaking on an exception? (This doesn't work because there is no way to pinpoint where the exception has come from. The stack trace only points to the third party code, not my calling code.)
Don't stop debugger at THAT exception when it's thrown and caught (This results in the aforementioned bubbling and circumvention.)
Using VS 2022