Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio - suppress certain "Exception thrown" messages

Tags:

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

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.

like image 825
František Němec Avatar asked Oct 19 '16 09:10

František Němec


People also ask

How do I break an exception in Visual Studio?

Tell the debugger to break when an exception is thrownIn the Exception Settings window (Debug > Windows > Exception Settings), expand the node for a category of exceptions, such as Common Language Runtime Exceptions. Then select the check box for a specific exception within that category, such as System.

How do I stop Visual Studio from breaking on exception?

To turn off stop on exceptions press " Ctrl + Alt + E ". This will open the Exceptions window . Untick "Common Language Runtime Exceptions - Thrown". That would prevent it from pausing from within the delegate, but not when it's rethrown on Wait .

Is throwing exception good practice?

The short answer is NO. You would throw an exception if the application can't continue executing with the bad data. In your example, the logic is to display an error message on the front end and Option 2 is the cleaner method for achieving this requirement.


1 Answers

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.

like image 170
Jack Zhai-MSFT Avatar answered Oct 05 '22 13:10

Jack Zhai-MSFT