Throw single custom exception from Class Library
.net, c#, design-patterns
Solution
You could use:
AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
{
var exception = e.ExceptionObject as Exception;
if (exception != null && exception.Source == "MyLib")
{
// Do stuff here
}
};
But seriously, don't do it. Exceptions shouldn't be handled this way, you should catch them locally.
Problem
How can I write a catch all exception handler in a Class Library project in C#, which will give the outside caller only one Custom Exception on any exception occurring in the library. Most nearest solution I found is to implement a `Facade` class, call low level classes from it, and write `try..catch` in every call from Facade which will throw single custom exception if any exception occurs underneath. I searched for solutions, but got it only for Web Applications (using Application context for catching exceptions) e.g. How to implement one "catch'em all" exception handler with resume? I need to implement it at Class Library level or at consumer of library level, but good if that can be done by write minimum exception handling statements.