Visual Studio - 禁止某些“Exception thrown”消息

时间:2016-10-19 09:41:17

标签: c# visual-studio debugging exception

您可以在输出中为某些方法(某些代码区域)隐藏“Exception thrown”消息吗?

我使用HttpWebRequest进行服务器通信。我会定期检查服务器是否可用(每秒几次)。当服务器无法访问时,HttpWebRequest会抛出异常。我抓住它并将GUI元素设置为false。问题是当服务器无法访问时,输出窗口会被“Exception thrown”消息弄得乱七八糟。

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

我需要的例子:

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

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

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

编辑:

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

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

2 个答案:

答案 0 :(得分:7)

要禁用异常消息:

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

(2)您也可以在TOOLS->选项 - >调试 - >输出窗口下禁用它。

(3)或者你可以使用Debug菜单下的Exception Settings - > Windows-> Exception Settings来抛出Exception。

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

没有其他好的建议,但我帮助您在此处提交功能:https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/16752127-visual-studio-suppress-certain-exception-thrown-me

你可以投票。

答案 1 :(得分:0)

如果您愿意稍等一下或使用预发布版本,则下一版本的Visual Studio(VS 15)将具有“Add Conditions to Exception Settings”功能

  

将条件添加到例外设置

配置时   调试器打破抛出的异常,你可以添加条件   只有在指定的异常中抛出异常时,调试器才会中断   模块。

     

Edit Conditions dialog box

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