抑制第一次机会例外 [英] Suppress first chance exceptions

查看:181
本文介绍了抑制第一次机会例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Visual Studio(C#调试器)中抑制特定代码行的第一次机会抑制?

Is it possible to suppress first chance supressions in Visual Studio (C# debugger) for specific lines of code?

我想在调试器中使用第一次机会异常,但是在我得到有趣的代码之前,我需要经历每个调试会话约50个第一次机会异常。

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.

推荐答案

DebuggerNonUserCodeAttribute Class



从.NET 2.0开始,如果您使用 [ DebuggerNonUserCode ] 属性,调试器将跳过其中的第一次机会异常。

DebuggerNonUserCodeAttribute Class

As of .NET 2.0, if you mark an method with the [DebuggerNonUserCode] attribute, the debugger will skip first chance exceptions in it.

从MSDN链接引用(重点是我的):

Quote from MSDN link (emphasis added is mine):


成员
不是代码的一部分
sp用户可以通过自己创建的,可以使
使调试体验复杂化。
此属性禁止在
调试器窗口中显示这些附件类型和成员的
,并自动
通过
步骤,而不是
设计师提供的代码。

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.

但是,如果您只有一种方法用于包含在Visual Studio的第一次机会异常处理机制中的某些行,以及要排除的其他行,则可能没有解决方案这个级别的粒度。您可以随时将大型方法重构为多个方法,并在选定的方法上使用该属性。

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.

其他信息...

这篇文章的使用示例

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;
    }
}

文章显示可能有用的相关VS项目选项。

The article shows related VS project options that might be useful.

这篇关于抑制第一次机会例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆