Skip to content

Commit

Permalink
Moved Unhandled Exception Handler to App.xaml.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Timthreetwelve committed Jun 28, 2024
1 parent 4b44dd1 commit 894e809
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
31 changes: 31 additions & 0 deletions GetMyIP/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

// Unhandled exception handler
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

// Only allows a single instance of the application to run.
SingleInstance.Create(AppInfo.AppName);

Expand Down Expand Up @@ -165,4 +168,32 @@ protected override void OnStartup(StartupEventArgs e)
}
}
}

#region Unhandled Exception Handler
/// <summary>
/// Handles any exceptions that weren't caught by a try-catch statement.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <remarks>
/// This uses default message box.
/// </remarks>
internal static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
_log.Error("Unhandled Exception");
Exception e = (Exception)args.ExceptionObject;
_log.Error(e.Message);
if (e.InnerException != null)
{
_log.Error(e.InnerException.ToString());
}
_log.Error(e.StackTrace);

string msg = string.Format($"{GetStringResource("MsgText_Error")}\n{e.Message}");
_ = MessageBox.Show(msg,
"Get My IP ERROR",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
#endregion Unhandled Exception Handler
}
35 changes: 2 additions & 33 deletions GetMyIP/Helpers/MainWindowHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace GetMyIP.Helpers;
internal static class MainWindowHelpers
{
#region Startup
internal static async Task GetMyIPStartUp(MainWindowHelpers mainWindowHelpers)
internal static async Task GetMyIPStartUp()
{
EventHandlers();

if (!App.LogOnly)
{
mainWindowHelpers.ApplyUISettings();
ApplyUISettings();

string returnedJson = await IpHelpers.GetAllInfoAsync();
IpHelpers.ProcessProvider(returnedJson, false);
Expand Down Expand Up @@ -123,9 +123,6 @@ public static string WindowTitleVersionAdmin()
/// </summary>
internal static void EventHandlers()
{
// Unhandled exception handler
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

// Settings change events
UserSettings.Setting!.PropertyChanged += SettingChange.UserSettingChanged!;
TempSettings.Setting!.PropertyChanged += SettingChange.TempSettingChanged!;
Expand Down Expand Up @@ -277,34 +274,6 @@ public static T FindParent<T>(DependencyObject child) where T : DependencyObject
}
#endregion Find a parent of a control

#region Unhandled Exception Handler
/// <summary>
/// Handles any exceptions that weren't caught by a try-catch statement.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
/// <remarks>
/// This uses default message box.
/// </remarks>
internal static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
_log.Error("Unhandled Exception");
Exception e = (Exception)args.ExceptionObject;
_log.Error(e.Message);
if (e.InnerException != null)
{
_log.Error(e.InnerException.ToString());
}
_log.Error(e.StackTrace);

string msg = string.Format($"{GetStringResource("MsgText_Error")}\n{e.Message}");
_ = MessageBox.Show(msg,
"Get My IP ERROR",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
#endregion Unhandled Exception Handler

#region Set theme
/// <summary>
/// Gets the current theme
Expand Down

0 comments on commit 894e809

Please sign in to comment.