Is it possible to suppress first chance supressions in Visual Studio (C# debugger) for specific lines of code?
I want to use first chance exceptions in the debugger, but there are about 50 first chance exceptions I need to go through every debug session before I get to the interesting code.
Currently, I turn off first chance exceptions and then manually turn them on, but that's a hassle and a time sink.
A first chance exception occurs when an exception is thrown and there is no catch block to handle it. Enablying first chance exceptions stops the debugger whenever an exception is thrown whether there is a catch block or not.
When an application is being debugged, the debugger gets notified whenever an exception is encountered At this point, the application is suspended and the debugger decides how to handle the exception. The first pass through this mechanism is called a "first chance" exception.
As of .NET 2.0, if you mark an method with the [DebuggerNonUserCode] attribute, the debugger will skip first chance exceptions in it.
Quote from MSDN link (emphasis added is mine):
members that are not part of the code specifically created by the user can complicate the debugging experience. This attribute suppresses the display of these adjunct types and members in the debugger window and automatically steps through, rather than into, designer provided code.
There is no runtime behaviour apart from debugging, associated with this attribute.
However if you have just one method with certain lines intended for inclusion in Visual Studio's first chance exception handling mechanism, and other lines to be excluded, there's likely not a solution at this level of granularity. You can always refactor a large method into multiple methods and use the attribute on select ones.
Additional Info...
Example usage from this article
using System.Diagnostics; using XL = Microsoft.Office.Interop.Excel; public static class WorkbookExtensions { [DebuggerNonUserCode] public static bool TryGetWorksheet(this XL.Workbook wb, string worksheetName, out XL.Worksheet retrievedWorksheet) { bool exists = false; retrievedWorksheet = null; try { retrievedWorksheet = GetWorksheet(wb, worksheetName); exists = retrievedWorksheet != null; } catch(COMException) { exists = false; } return exists; } [DebuggerNonUserCode] public static XL.Worksheet GetWorksheet(this XL.Workbook wb, string worksheetName) { return wb.Worksheets.get_Item(worksheetName) as XL.Worksheet; } }
The article shows related VS project options that might be useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With