Catch all the exceptions and do ... nothing?

I just inherited code that has over 300 events:

catch(Exception ex)
{
    string s = ex.Message ;
}

The next time I came across a person who wrote this, what should I do with them?

But seriously ...

This is obviously a coding horror and one of the worst things a programmer can do. Do I have to go through and just delete all this and see how bad it really is when I run the application? How would you decide to fix it wrong?

This is a WinForms application that runs inside my organization for about 2 dozen users.

+3
source share
10 answers

You can remove all of these catch blocks and add the following code before starting your first form:

public static void Main(string[] args)
{
// Event handler for handling UI thread exceptions.
Application.ThreadException += 
    new ThreadExceptionEventHandler(App_UiThreadException);

// Force all Windows Forms errors to go through our handler.
// NB In .NET 4, this doesn't apply when the process state is corrupted.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Event handler for handling non-UI thread exceptions. 
AppDomain.CurrentDomain.UnhandledException += new 
    UnhandledExceptionEventHandler(App_NonUiThreadException);

// Run the application.
}

Interception of all events

SetUnhandledExceptionMode , .

, .NET Framework 4 , , , , , - HandleProcessCorruptedStateExceptionsAttribute.

, .

Application.ThreadException ui. . ui, .

, UI

AppDomain.UnhandledException , ui. . , -ui- , , .

+5

, . , . - . , , .

+5

, , !

+3

, . , - , , .

?

  • , ( CTRL + ALT + E Visual Studio)
  • , , ( , )

, no-op; , . , , , .

+3

, dev - :

LogCrazyException(ex);
#if DEBUG
    throw;
#endif
+2

, :

catch(Exception ex) {
  throw;
}

try... try, , try... catch. , , , . / , , try... catch , .

+2

, , . MessageBox, .

+1

.. , , , , . , ...

0

, . , Control.BeginInvoke, , (*) , - . , , , BeginInvoke, , , , BeginInvoke, , . BeginInvoke, , , , , . , , , , , - ( ).

, 300 Try-Catch-Ignore. . BeginInvoke, catch, - TryBeginInvoke. , - , can't-do-anything-about . , 300 , , , 300 .

(*) It would be possible to use locks to protect BeginInvoke and Dispose methods, but this adds considerable complexity and creates additional opportunities for something to go wrong. If the .net TryBeginInvoke method existed, that would be beautiful. Since it does not exist, it seems that the best approach is to record such a procedure for wrapping BeginInvoke using the Try-Catch-Ignore block.

0
source
try{

}
catch (Exception)
{
}

prevent warning messages

0
source

Source: https://habr.com/ru/post/1795561/


All Articles