Finding Unhandled Exceptions - .NET

I have a pretty big .NET solution with poor exception handling. The original authors had in mind that catching certain types of exceptions, and not the base class of exceptions, but this led to the fact that I was able to calculate the places where type exceptions are excluded, but not caught anywhere and makes the software unreliable. How can I perform code analysis to determine where the type of exception may be thrown in the solution but not caught?

+4
source share
4 answers

You can add a global exception handler that logs exception traces, including stack traces, and creates a nice error dialog for the user. You can use stack tracing to identify places in the code that generates errors and more efficiently handle these scripts. Register as much as possible.

How you do this depends on the platform (WebForms, MVC, WinForms, WPF, ...?)

In addition, an in-depth review of the code throughout the code base with an emphasis on error handling can take a lot of errors before they manifest themselves as errors for the user.

+2
source

This is how I process them. In my Program.cs in the Main method, I configured event handling. After that, you put the code that you need into these events.

 Application.ThreadException += Form1_UIThreadException; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 

EDIT: This is for apps other than websites.

+1
source

Any exception that is not caught is not processed. The good news is that you can subscribe to this AppDomain.CurrentDomain.UnhandledException event.

Throws any exception that is not thrown.

How can I perform code analysis to determine where in the solution the type of exception can be thrown, but not caught?

There are programs like StyleCop that highlight possible unhandled exceptions.

0
source

In addition to what is mentioned in the answers, you can also use tools like Pex. This allows you to automatically create a test suite that covers many corner cases of your code.

Take a look

0
source

All Articles