Search code examples
c#debuggingerror-handlingxnapublish

How to handle C# XNA errors and debug optimally with published games?


I've been developing my first game and after publishing it to create en executable and giving it to my friend to test, he keeps getting crashes and there is no error report for me to find out why. This doesn't happen on my computer and the book I learned from never taught me about how to handle errors when you publish your game. I searched online and the best things I've found have been about using the catch{} and run{} methods, but then I'd have to encase everything in them. Can someone please tell me an optimal way to handle errors with published games? Is there a method or something that can write errors/exceptions to a text file upon crash or something? Thankyou in advance!


Solution

  • That's what logs are for!

    Here's an example on MSDN to write to a file in C#.

    You shouldn't encase all your code in Try - Catch blocks, it's really a bad practice. Instead, encase parts of the code that are likely to crash.

    You can keep a log of your errors in a file with the error message and stack trace. Here's another article on logging in C#.

    You will most likely end up with something like this:

    Try
    {
        //Some code that may crash
    }
    Catch(Exception ex)
    {
        WriteToLog(ex.Message, ex.StackTrace);
        throw;
    }