Skip to content

Commit

Permalink
Add a bit more try/catch for logging errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinematics committed Oct 31, 2015
1 parent dc523b8 commit 3484fa4
Showing 1 changed file with 54 additions and 38 deletions.
92 changes: 54 additions & 38 deletions NetTally/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,53 @@ public string MyTitle
/// </summary>
public MainWindow()
{
// Set up an event handler for any otherwise unhandled exceptions in the code.
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
try
{
// Set up an event handler for any otherwise unhandled exceptions in the code.
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

InitializeComponent();
InitializeComponent();

// Set tally vars
tally = new Tally();
// Set tally vars
tally = new Tally();

questCollection = new QuestCollection();
questCollection = new QuestCollection();

QuestCollectionWrapper wrapper = new QuestCollectionWrapper(questCollection, null, DisplayMode.Normal);
QuestCollectionWrapper wrapper = new QuestCollectionWrapper(questCollection, null, DisplayMode.Normal);

NetTallyConfig.Load(tally, wrapper);
NetTallyConfig.Load(tally, wrapper);

// Set up view for binding
QuestCollectionView = CollectionViewSource.GetDefaultView(questCollection);
// Sort the collection view
var sortDesc = new SortDescription("DisplayName", ListSortDirection.Ascending);
QuestCollectionView.SortDescriptions.Add(sortDesc);
// Set the current item
QuestCollectionView.MoveCurrentTo(questCollection[wrapper.CurrentQuest]);
// Set up view for binding
QuestCollectionView = CollectionViewSource.GetDefaultView(questCollection);
// Sort the collection view
var sortDesc = new SortDescription("DisplayName", ListSortDirection.Ascending);
QuestCollectionView.SortDescriptions.Add(sortDesc);
// Set the current item
QuestCollectionView.MoveCurrentTo(questCollection[wrapper.CurrentQuest]);

Properties.Settings settings = new Properties.Settings();
tally.DisplayMode = wrapper.DisplayMode;
Properties.Settings settings = new Properties.Settings();
tally.DisplayMode = wrapper.DisplayMode;

// Set up data contexts
DataContext = QuestCollectionView;
// Set up data contexts
DataContext = QuestCollectionView;

resultsWindow.DataContext = tally;
tallyButton.DataContext = tally;
cancelTally.DataContext = tally;
displayMode.DataContext = tally;
newRelease.DataContext = checkForNewRelease;
resultsWindow.DataContext = tally;
tallyButton.DataContext = tally;
cancelTally.DataContext = tally;
displayMode.DataContext = tally;
newRelease.DataContext = checkForNewRelease;


var assembly = Assembly.GetExecutingAssembly();
var product = (AssemblyProductAttribute)assembly.GetCustomAttribute(typeof(AssemblyProductAttribute));
var version = (AssemblyInformationalVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute));
MyTitle = $"{product.Product} - {version.InformationalVersion}";
var assembly = Assembly.GetExecutingAssembly();
var product = (AssemblyProductAttribute)assembly.GetCustomAttribute(typeof(AssemblyProductAttribute));
var version = (AssemblyInformationalVersionAttribute)assembly.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute));
MyTitle = $"{product.Product} - {version.InformationalVersion}";
}
catch (Exception e)
{
string file = ErrorLog.Log(e);
MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Error in startup", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

/// <summary>
Expand All @@ -111,7 +119,7 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE

string file = ErrorLog.Log(ex);

MessageBox.Show($"Error log written to:\n{file ?? "(unable to write log file)"}", "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Unhandled exception", MessageBoxButton.OK, MessageBoxImage.Error);
}

/// <summary>
Expand All @@ -121,18 +129,26 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE
/// <param name="e"></param>
private void Window_Closing(object sender, CancelEventArgs e)
{
string selectedQuest = "";

if (CurrentlySelectedQuest() != null)
try
{
selectedQuest = CurrentlySelectedQuest().ThreadName;
}
string selectedQuest = "";

if (CurrentlySelectedQuest() != null)
{
selectedQuest = CurrentlySelectedQuest().ThreadName;
}

QuestCollectionWrapper qcw = new QuestCollectionWrapper(questCollection, selectedQuest, tally.DisplayMode);
NetTallyConfig.Save(tally, qcw);
QuestCollectionWrapper qcw = new QuestCollectionWrapper(questCollection, selectedQuest, tally.DisplayMode);
NetTallyConfig.Save(tally, qcw);

Properties.Settings settings = new Properties.Settings();
settings.Save();
Properties.Settings settings = new Properties.Settings();
settings.Save();
}
catch(Exception ex)
{
string file = ErrorLog.Log(ex);
MessageBox.Show($"Error log saved to:\n{file ?? "(unable to write log file)"}", "Error in shutdown", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion

Expand Down

0 comments on commit 3484fa4

Please sign in to comment.