抑制第一次机会异常 [英] Suppress first chance exceptions

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

问题描述

是否可以在 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):

会员不是代码的一部分由用户专门创建可以使调试体验复杂化.该属性抑制显示这些附属类型和成员在调试器窗口并自动逐步通过,而不是进入,设计者提供的代码.

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天全站免登陆