简体   繁体   中英

Visual Studio - suppress certain "Exception thrown" messages

Can you hide "Exception thrown" messages in output for certain methods (certain code areas)?

I use HttpWebRequest for server communication. I periodically check if the server is available (a few times every second). When a server is not reachable HttpWebRequest throws an exception. I catch it and set GUI elements enabled to false. The problem is when the server is unreachable, output window gets cluttered up with "Exception thrown" messages.

I know you can right-click output window and uncheck "Exception Messages". But I am not only one working on the project and there might be someone who wants to see some other exception messages (in their part of the project).

Example of what I need:

// 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();
} 

Current output:

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

Desired output:

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

Edit:

Enabling Just my code in Tools/Options/Debugging might help.

We used Npgsql to access PostgreSQL database and some calls had timeout. Everytime call timeouted "Exception thrown" was written to output window (and there were a lot). Just my code prevents that.

To disable the Exception messages:

(1)Like your previous reply, you could disable it in the Output windows.

(2)You could also disable it under TOOLS->Options->Debugging->Output Window.

(3)Or you could just throw the Exception using the Exception Settings under Debug menu->Windows->Exception Settings.

I don't find other workaround to disable it unless you really resolve/handle the Exceptions in your code. I test it using the VS2015 version.

No other good suggestion, but I help you submit a feature here: https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/16752127-visual-studio-suppress-certain-exception-thrown-me

You could vote it.

If you are willing to wait a little bit or use a pre-release version, the next version of Visual Studio (VS 15) will have a feature " Add Conditions to Exception Settings "

Add Conditions to Exception Settings

When you configure the debugger to break on thrown exceptions, you can add conditions so that the debugger will only break when exceptions are thrown in specified modules.

编辑条件对话框

This will allow you to set filters on when exceptions should break.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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