0

I have an executable that is written in C# which runs fine on one computer but crashes without providing an error on another computer.

Is there a class that I can add to my code that will dump all the information relating to the crash no matter where the error occurs within the code?

I have seen this post but I was hoping to create a "catch all" error handling class that would exist in my code.

Community
  • 1
  • 1
user1423893
  • 766
  • 4
  • 15
  • 26
  • Debug the project on that "other" pc and check where the error occurs. – Max Jan 05 '14 at 13:38
  • You haven't stated what kind of application this is. Global exception handling in ASP.NET applications is generally different to WinForms applications, for example. – Ian Newson Jan 05 '14 at 13:40
  • Apologies. This is an XNA Framework 4.0 project which also contains WinForms code for loading and saving files. – user1423893 Jan 05 '14 at 14:08

1 Answers1

2

Try the AppDomain exception handler:

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx

Code sample:

class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            var thr = new Thread(() =>
            {
                Thread.Sleep(1000);
                throw new Exception("Custom exception from thread");
            });
            thr.Start();
            thr.Join();

            Console.WriteLine("Done");
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //Log information from e.ExceptionObject here
        }
    }

In this example a custom global exception handler is registered, and then a thread is started which throws an exception after 1 second. The global exception handler is then invoked, with the custom exception that has been thrown.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • I wonder if this works with an application that hosts multiple AppDomains – Leo Jan 05 '14 at 15:46
  • @Leo no, it doesn't appear to. However if you're creating further app domains it's normally trivial to register the same unhandled exception handler for each of them. – Ian Newson Jan 05 '14 at 16:05