changeset 47:07fd9f7b0dd1

Re #79: Make use of GTK# callback for exceptions * Hook up method to event * Add dialog on unhandled exception so that the user knows Note: Still prints lots of stack trace about GLib stuff, but I don't know if that's normal yet
author IBBoard <dev@ibboard.co.uk>
date Sat, 14 Aug 2010 19:56:08 +0000
parents 3314f6a46661
children 23238d998535
files FrmMainWindow.cs
diffstat 1 files changed, 28 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/FrmMainWindow.cs	Sat Aug 14 19:25:08 2010 +0000
     1.2 +++ b/FrmMainWindow.cs	Sat Aug 14 19:56:08 2010 +0000
     1.3 @@ -27,6 +27,7 @@
     1.4  using IBBoard.WarFoundry.Plugin.Rollcall;
     1.5  using IBBoard.Xml;
     1.6  using log4net;
     1.7 +using GLib;
     1.8  
     1.9  namespace IBBoard.WarFoundry.GTK
    1.10  {
    1.11 @@ -53,10 +54,11 @@
    1.12  
    1.13  		private MenuToolButton undoMenuButton, redoMenuButton;
    1.14  
    1.15 -		public static void Main (string[] args)
    1.16 +		public static void Main(string[] args)
    1.17  		{
    1.18  			try
    1.19  			{
    1.20 +				ExceptionManager.UnhandledException += HandleUnhandledException;
    1.21  				Application.Init();
    1.22  				FrmMainWindow win = new FrmMainWindow(args);
    1.23  				win.Show();
    1.24 @@ -65,10 +67,34 @@
    1.25  			}
    1.26  			catch(Exception ex)
    1.27  			{
    1.28 -				LogManager.GetLogger(typeof(FrmMainWindow)).Fatal("("+ex.GetType().Name+") "+ex.Message + Environment.NewLine + ex.StackTrace);
    1.29 +				HandleUnhandledException(ex);
    1.30  			}
    1.31  		}
    1.32  
    1.33 +		private static void HandleUnhandledException(UnhandledExceptionArgs args)
    1.34 +		{
    1.35 +			object obj = args.ExceptionObject;
    1.36 +			Exception ex = null;
    1.37 +			
    1.38 +			if (obj is Exception)
    1.39 +			{
    1.40 +				ex = (Exception)obj;
    1.41 +			}
    1.42 +			else
    1.43 +			{
    1.44 +				ex = new Exception("GLib returned unexpected exception object type "+obj.GetType());
    1.45 +			}
    1.46 +			
    1.47 +			HandleUnhandledException(ex);
    1.48 +		}
    1.49 +
    1.50 +		private static void HandleUnhandledException(Exception ex)
    1.51 +		{
    1.52 +			LogManager.GetLogger(typeof(FrmMainWindow)).Fatal("(" + ex.GetType().Name + ") " + ex.Message + Environment.NewLine + ex.StackTrace);
    1.53 +			MessageDialog dialog = new MessageDialog(null, DialogFlags.Modal, MessageType.Error, ButtonsType.Ok, false, "An unhandled exception occurred. Please check the log for more details.");
    1.54 +			dialog.Show();
    1.55 +		}
    1.56 +
    1.57  		public FrmMainWindow() : this(new string[0])
    1.58  		{
    1.59  			//Do nothing extra