/// <summary> /// Attempt to handle/report an exception. /// </summary> /// <param name="ex">Exception to handle.</param> /// <returns><c>true</c> if the exception was handled, <c>false</c> if it should be rethrown.</returns> public bool TryHandleException(Exception ex) { var args = new DispatchExceptionEventArgs(ex, false); HandleException(args); return(args.ShouldRethrow); }
private void HandleException(DispatchExceptionEventArgs e) { if (PreviewException != null) { PreviewException(this, e); } if (e.ShouldReport) { _logger.WriteError(e.IsUnhandled ? "Unhandled exception: {0}\r\n{1}" : "Exception: {0}\r\n{1}", e.Exception.Message, e.Exception.StackTrace); if (_reportHandler != null) { _reportHandler(this, e); } } if (e.ShouldTerminate && !_isTerminating) { _logger.WriteWarning("Terminating application"); _isTerminating = true; Application.Current.Shutdown(); } }
private static void DefaultExceptionHandler(object sender, DispatchExceptionEventArgs e) { string title; try { var dialogService = ServiceRepository.Instance.FindService <IDialogService>(); title = dialogService.MainWindow.Title; } catch (Exception) { try { title = Application.Current.MainWindow.Title; } catch (Exception) { try { title = Assembly.GetEntryAssembly().GetName().Name; } catch (Exception) { title = String.Empty; } } } var innerException = e.Exception; while (innerException.InnerException != null) { innerException = innerException.InnerException; } try { var logService = ServiceRepository.Instance.FindService <ILogService>(); var logger = logService.GetLogger("Jamiras.Core"); logger.WriteError(innerException.Message + "\n" + innerException.StackTrace); } catch { // ignore exception trying to log exception } if (title.Length > 0) { title += " - "; } if (e.IsUnhandled) { title += "Unhandled "; } title += innerException.GetType().Name; MessageBox.Show(innerException.Message, title, MessageBoxButton.OK, MessageBoxImage.Error); }
private void DispatchUnhandledException(object sender, UnhandledExceptionEventArgs e) { if (e.IsTerminating) { _isTerminating = true; } var args = new DispatchExceptionEventArgs((Exception)e.ExceptionObject, true); HandleException(args); }