Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch all un-handled exceptions in a .net assembly (library type, not application)

I have an assembly containing a number of classes. This is a class library type assembly, not a windows forms application. It's also single threaded.

Is there a way to catch all un-handled exceptions so that I can log them?

like image 716
Jules Avatar asked Dec 08 '25 15:12

Jules


1 Answers

In my opinion, putting such logic inside library is not a good idea. I think it should be application responsibility to decide how to deal with exceptions (both handled and unhandled). However you might look at AppDomain.UnhandledException. You can install such handler for CurrentDomain and do something there. However doing this way you are restricting usages of you library (for example implying that library would be used only in one domain). Also you will receive notifications for all unhandled exceptions even totally unrelated to your assembly.

I think that better idea is to allow developers that use your library to do their job dealing with all unhandled exceptions (possibly with UnhandledException installed by the application).

like image 123
SergGr Avatar answered Dec 10 '25 04:12

SergGr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!