Global Exception Handling
-
I have to following in my App.xaml code behind:
public partial class App : Application
{
public App()
{
}private void Application\_Startup(object sender, StartupEventArgs e) { Application.Current.DispatcherUnhandledException += dispatchUnhandled; AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException; } private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e) { var errorMessage = getInnerException(e.Exception); displayErrorMessage(errorMessage, e.Exception); e.Handled = true; } private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; var errorMessage = getInnerException(ex); displayErrorMessage(errorMessage, ex); } private static void displayErrorMessage(string errorMessage, Exception ex) { logException(errorMessage, ex); ErrorHandlerViewModel vm = new ErrorHandlerViewModel(); vm.ErrorMessage = errorMessage; vm.ErrorText = ex.ToString(); ErrorHandlerView view = new ErrorHandlerView(); view.DataContext = vm; view.ShowDialog(); Application.Current.Shutdown(); } private static string getInnerException(Exception e) { string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException); return msg; } private static void logException(string errorMessage, Exception ex) { Logger.LogMessage(errorMessage); Logger.LogMessage(ex.ToString()); }
}
Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks
If it's not broken, fix it until it is
-
I have to following in my App.xaml code behind:
public partial class App : Application
{
public App()
{
}private void Application\_Startup(object sender, StartupEventArgs e) { Application.Current.DispatcherUnhandledException += dispatchUnhandled; AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException; } private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e) { var errorMessage = getInnerException(e.Exception); displayErrorMessage(errorMessage, e.Exception); e.Handled = true; } private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; var errorMessage = getInnerException(ex); displayErrorMessage(errorMessage, ex); } private static void displayErrorMessage(string errorMessage, Exception ex) { logException(errorMessage, ex); ErrorHandlerViewModel vm = new ErrorHandlerViewModel(); vm.ErrorMessage = errorMessage; vm.ErrorText = ex.ToString(); ErrorHandlerView view = new ErrorHandlerView(); view.DataContext = vm; view.ShowDialog(); Application.Current.Shutdown(); } private static string getInnerException(Exception e) { string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException); return msg; } private static void logException(string errorMessage, Exception ex) { Logger.LogMessage(errorMessage); Logger.LogMessage(ex.ToString()); }
}
Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks
If it's not broken, fix it until it is
Are any exceptions thrown? If so, where are they thrown from?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Are any exceptions thrown? If so, where are they thrown from?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
yes, I added this in the MainWindowViewModel:
int x = 0;
int y = 10;
int z = y / x;It throws, it doesn't get caught in the global exception handler.
If it's not broken, fix it until it is
-
yes, I added this in the MainWindowViewModel:
int x = 0;
int y = 10;
int z = y / x;It throws, it doesn't get caught in the global exception handler.
If it's not broken, fix it until it is
What triggers that code to run?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
What triggers that code to run?
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
It's the main window in a WPF app. It's called from App.xaml
If it's not broken, fix it until it is
-
I have to following in my App.xaml code behind:
public partial class App : Application
{
public App()
{
}private void Application\_Startup(object sender, StartupEventArgs e) { Application.Current.DispatcherUnhandledException += dispatchUnhandled; AppDomain.CurrentDomain.UnhandledException += applicationUnhandledException; } private void dispatchUnhandled(object sender, DispatcherUnhandledExceptionEventArgs e) { var errorMessage = getInnerException(e.Exception); displayErrorMessage(errorMessage, e.Exception); e.Handled = true; } private static void applicationUnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; var errorMessage = getInnerException(ex); displayErrorMessage(errorMessage, ex); } private static void displayErrorMessage(string errorMessage, Exception ex) { logException(errorMessage, ex); ErrorHandlerViewModel vm = new ErrorHandlerViewModel(); vm.ErrorMessage = errorMessage; vm.ErrorText = ex.ToString(); ErrorHandlerView view = new ErrorHandlerView(); view.DataContext = vm; view.ShowDialog(); Application.Current.Shutdown(); } private static string getInnerException(Exception e) { string msg = e.InnerException == null ? e.Message : getInnerException(e.InnerException); return msg; } private static void logException(string errorMessage, Exception ex) { Logger.LogMessage(errorMessage); Logger.LogMessage(ex.ToString()); }
}
Application_Startup DOES run, but no exceptions are caught. What's wrong here? Thanks
If it's not broken, fix it until it is
When an unhandled exception is thrown Application.Current.DispatcherUnhandledException is invoked. Hence the code written in your handler 'dispatchUnhandled' is executed. Do i miss your specific question?
Manish Jain (MJ)