Introduction
While working on a network based editor extension I had to figure out how to go about handling any errors/crashes that may be caused by my plugin and having a way of handling it for the end user. Ideally when maintaining a product you’ll want to know when you users run into errors caused by your program and what errors were encountered so you can fix them for a future update. I’m going to go through things you should be checking off before releasing your buggy code to the wild.
Logging
You should be logging user’s actions throughout your program into some location. For me I was able to just keep a list of strings, but depending on your application you may need to store it locally on some kind of text file. Logs should also be prefaced with a time stamp and a tag that marks what file the log came from to make it easier to look up when looking through them.
If your application calls for it, screenshots/videos are also good ways of keeping track of things. Taking a quick screenshot and saving that when you encounter something important isn’t unreasonable, or a quick video of a process works well.
Depending on your needs as well it can be important to log information about the end user’s device as well, for example their IP/MAC address, GPU/CPU, RAM, current memory usage, screen , window dimensions, etc. Anything that can be related to your program.
Catching exceptions is also important. Whenever an error/exception is thrown your IDE will log this but how do you go about getting that exception? Simply surround your code with try { } then after add catch(Exception e) { } after the try expression, like below.
try
{
StreamWriter sw = new StreamWriter("example.txt");
StreamReader sr = new StreamReader("example.txt");
} catch (FileNotFoundException e) { Log("Could not find: example.txt, " + e.ToString()); } catch (AccessViolationException e) { Log("Access violation! " + e.ToString()); }
catch (Exception e)
{
Log(e.ToString());
}
Here we will always get an exception because you cannot write and read from the same file at the same time, you’d need to close one. Because we have catch, we will be able to access that error. Now we can add it to our logs. There are different types of Exceptions that you can catch but the Exception class is the base class all exceptions inherit from and will be a catch-all. Check MSDN for more information on exceptions.
Crash Reporting
Crash/Error Screen
Conclusion