We have a global exception handling system in place we've been using for years. It gives us a chance to capture info and log it, and even let the user add their comments of what they were doing, all in a custom error window.
I recently tried adding a custom exception (as shown in answer) to easily format the DbEntityValidationErrors so they would be displayed and logged properly so developers can fix the issue.
I then over rode the SaveChanges() of my DbContext like this:
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException e)
{
throw new FormattedDbEntityValidationException(e);
}
}
However when debugging, this exception in not being handled like all other exceptions. I can go add this line throw new Exception("this is a test"); anywhere in my ViewModels and they get properly caught by our global handling.
Application.Current.DispatcherUnhandledException += AppDispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += TaskSchedulerUnobservedTaskException;
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomainUnhandledException);
These have always seemed to handle catching unhandled exceptions. Which leads me to believe either:
- There is some
catchstatement upstream consuming it - An issue with my custom exception class
The first seems the most likely, but how do I figure out what catch is consuming it upstream? I've followed the call stack and followed code and cannot find it.
My custom exception class is in a shared library used by my main app and all the modules, so it should be accessible and part of the AppDomain.
Is there any other reason my global exception handlers wouldn't catch this exception being thrown from the SaveChanges()?