选择性地忽略C#代码中引发的异常 [英] Selectively ignore thrown exceptions in C# code

查看:148
本文介绍了选择性地忽略C#代码中引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#代码中的函数,其中NullReferenceException被定期抛出(预期的行为),但是被捕获。有没有办法我可以告诉Visual Studio调试器不要打破这个代码的这个特定部分的这个例外?



我需要编辑打破我的代码中的这个异常,而不是在同一个函数中。

解决方案

如果我正确理解,尝试做的是调试一些NullReferenceException,但是要在调试时暂时忽略其他人,您可以通过标记要使调试器忽略的功能来执行此操作,使用DebuggerNonUserCode 属性。

  [DebuggerNonUserCode] 
private void MyMethod()
{
// NullReferenceException异常捕获此方法将
//不会导致Debugger在此停止..
}

注意所谓的方法。这只是说,如果调试器设置为始终在 NullReferenceException 异常中断,调试器不会导致调试器中断。而且这只适用于方法,而不是方法中的任意代码段。


I have a function in C# code where a NullReferenceException is thrown periodically (expected behavior), but caught. Is there a way I can tell the Visual Studio debugger to not break on this exception for this particular section of my code?

EDIT I need to break on this exception elsewhere in my code, but not in the same function.

解决方案

If I understand correctly and what you're trying to do is debug some NullReferenceException(s) but want to temporarily ignore others while debugging, you might be able to do this by marking functions that you want the debugger to ignore with the DebuggerNonUserCode attribute.

[DebuggerNonUserCode]
private void MyMethod()
{
    // NullReferenceException exceptions caught in this method will
    //  not cause the Debugger to stop here..
}

NOTE that this will only work if the exceptions are caught in said methods. It's just that they won't cause the debugger to break if you have the debugger set to always break on NullReferenceException exceptions. And that this only works on methods, and not arbitrary sections of code inside of a method..

这篇关于选择性地忽略C#代码中引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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