/// <summary> /// Runs the given <seealso cref="Action"/> and handles all non-critical exceptions by showing an /// error dialog to the user. If the exception is critical, as determiend by <seealso cref="ErrorHandler.IsCriticalException(Exception)"/> /// then it is re-thrown as this could be that the process is not in a good state to continue executing. /// </summary> /// <param name="action"></param> public static void HandleExceptions(Action action) { try { action(); } catch (AggregateException ex) { Debug.WriteLine($"Uncaught aggregate exception: {ex.Message}"); if (ErrorHandler.ContainsCriticalException(ex)) { throw; } EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptUtils.ExceptionPrompt(ex); } catch (Exception ex) { Debug.WriteLine($"Uncaught exception: {ex.Message}"); if (ErrorHandler.IsCriticalException(ex)) { throw; } EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptUtils.ExceptionPrompt(ex); } }
#pragma warning disable VSTHRD100 // Avoid async void methods /// <summary> /// Runs the given <seealso cref="Task"/> and handles all non-critical exceptions by showing an /// error dialog to the user. If the exception is critical, as determined by /// <seealso cref="ErrorHandler.IsCriticalException(Exception)"/>, then it /// is re-thrown as this could be that the process is not in a good state to continue executing. /// </summary> /// <remarks> /// This method is intentionally async void, as it is meant to be the fire and forget method for tasks. /// </remarks> public static async void HandleExceptionsAsync(Func <Task> taskFunc) { try { await taskFunc(); } catch (Exception ex) when(!IsCriticalException(ex)) { EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptService.Default.ExceptionPrompt(ex); } }
/// <summary> /// Runs the given <seealso cref="Action"/> and handles all non-critical exceptions /// by showing an error dialog to the user. /// If the exception is critical, as determined by /// <seealso cref="ErrorHandler.IsCriticalException(Exception)"/>, then it /// is re-thrown as this could be that the process is not in a good state to continue executing. /// </summary> /// <param name="action"></param> public static void HandleExceptions(Action action) { try { action(); } catch (Exception ex) when(!IsCriticalException(ex)) { EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptService.Default.ExceptionPrompt(ex); } }
/// <summary> /// Runs the given <seealso cref="Task"/> and handles all non-critical exceptions by showing an /// error dialog to the user. If the exception is critical, as determiend by <seealso cref="ErrorHandler.IsCriticalException(Exception)"/> /// then it is re-thrown as this could be that the process is not in a good state to continue executing. /// </summary> public static async void HandleAsyncExceptions(Func <Task> task) { try { await task(); } catch (Exception ex) when(!IsCriticalException(ex)) { Debug.WriteLine($"Uncaught exception: {ex.Message}"); EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptUtils.ExceptionPrompt(ex); } }
/// <summary> /// Runs the given <seealso cref="Action"/> and handles all non-critical exceptions by showing an /// error dialog to the user. If the exception is critical, as determiend by <seealso cref="ErrorHandler.IsCriticalException(Exception)"/> /// then it is re-thrown as this could be that the process is not in a good state to continue executing. /// </summary> /// <param name="action"></param> public static void HandleExceptions(Action action) { try { action(); } catch (Exception ex) when(!IsCriticalException(ex)) { Debug.WriteLine($"Uncaught exception: {ex.Message}"); EventsReporterWrapper.ReportEvent(UnhandledExceptionEvent.Create(ex)); UserPromptUtils.ExceptionPrompt(ex); } }
/// <summary> /// Broadcast the exception /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _CurrentUnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { // we didn't come back from a broadcast if (_unhandled) { return; } _unhandled = true; var exception = new UnhandledExceptionEvent { Handled = e.Handled, UncaughtException = e.ExceptionObject }; EventAggregator.Publish(exception); e.Handled = exception.Handled; _unhandled = false; }
public static void ExceptionHandler() { var queue = new ManualEventQueue(); // create subscriber UnhandledExceptionEvent lastEventHandled = null; var subscriber = DelegateEventHandler.OnException(evnt => lastEventHandled = evnt); queue.Subscribers.Add(subscriber); // enqueue and handle event queue.Enqueue(new UnhandledExceptionEvent(new UnauthorizedAccessException())); Assert.Null(lastEventHandled); Assert.True(queue.HandleNext()); // test subscriber Assert.NotNull(lastEventHandled); Test.OrdinalEquals(lastEventHandled.Type, typeof(UnauthorizedAccessException).ToString()); // queue is empty again (no unhandled exceptions or anything else) Assert.False(queue.HandleNext()); }