繁体   English   中英

Visual Studio - 抑制某些“抛出异常”消息

[英]Visual Studio - suppress certain "Exception thrown" messages

您能否在某些方法(某些代码区域)的输出中隐藏“抛出异常”消息?

我使用 HttpWebRequest 进行服务器通信。 我会定期检查服务器是否可用(每秒几次)。 当服务器无法访问时,HttpWebRequest 会引发异常。 我抓住它并将启用的 GUI 元素设置为 false。 问题是当服务器无法访问时,输出窗口会因“抛出异常”消息而变得混乱。

我知道您可以右键单击输出窗口并取消选中“异常消息”。 但我不仅是从事该项目的人,而且可能有人想查看其他一些异常消息(在他们的项目中)。

我需要的示例:

// Keep showing "Exception thrown" message in this method.
static void Foo()
{
    try
    {
        throw new NotImplementedException();
    }
    catch (NotImplementedException ex)
    {
        // Process exception
    }
}

// Suppress "Exception thrown" message when it is thown in this method.
static void FooSuppress()
{
    try
    {
        throw new ArgumentException();
    }
    catch (ArgumentException ex)
    {
        // Process exception
    }
}

static void Main(string[] args)
{
    Foo();
    FooSuppress();
} 

电流输出:

Exception thrown: 'System.NotImplementedException' in ExceptionTest.dll
Exception thrown: 'System.ArgumentException' in ExceptionTest.dll

期望的输出:

Exception thrown: 'System.NotImplementedException' in ExceptionTest.dll

编辑:

工具/选项/调试中启用仅我的代码可能会有所帮助。

我们使用 Npgsql 访问 PostgreSQL 数据库,一些调用超时。 每次调用超时“抛出异常”被写入输出窗口(并且有很多)。 只是我的代码阻止了这种情况。

要禁用异常消息:

(1) 就像您之前的回复一样,您可以在输出窗口中禁用它。

(2)您也可以在 TOOLS->Options->Debugging->Output Window 下禁用它。

(3)或者您可以使用调试菜单->Windows->异常设置下的异常设置抛出异常。

除非您真正解决/处理代码中的异常,否则我找不到其他解决方法来禁用它。 我使用VS2015版本对其进行了测试。

没有其他好的建议,但我帮你在这里提交一个功能: https ://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/16752127-visual-studio-suppress-certain-exception-throw-

你可以投票。

如果您愿意稍等片刻或使用预发布版本,下一个版本的 Visual Studio (VS 15) 将具有“ 向异常设置添加条件”的功能

向异常设置添加条件

当您将调试器配置为在抛出异常时中断时,您可以添加条件,以便调试器仅在指定模块中抛出异常时才会中断。

编辑条件对话框

这将允许您在异常应该中断时设置过滤器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM