Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Throw Error Box instead of crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ElryMoe committed Sep 17, 2024
1 parent 3a957a7 commit f9e60a2
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ namespace AnilistListConverter;
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

// Add a global exception handler
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
Exception ex = (Exception)args.ExceptionObject;
ShowErrorAndShutdown(ex);
};

// Also catch UI thread exceptions
DispatcherUnhandledException += (sender, args) =>
{
Exception ex = args.Exception;
args.Handled = true;
ShowErrorAndShutdown(ex);
};
}

private void ShowErrorAndShutdown(Exception ex)
{
// Display the error in a message box
MessageBox.Show($"An unexpected error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

// Close the app after the user clicks OK
Environment.Exit(1);
}
}

0 comments on commit f9e60a2

Please sign in to comment.