What is the best way to collect crash data?

So, I am selling the concept of trying to automatically collect data from the program, i.e. turn out a dialog box that asks the user to send a report when something goes wrong.

I work in MS Visual Studio C #.

From an implementation point of view, does it make sense to put a try / catch loop in my main program.cs file, where does the application work? Like this:

try { Application.Run(new myMainForm()); } catch (Exception ex) { //the code to build the report I want to send and to //pop up the Problem Report form and ask the user to send } 

or does it make sense to lay try / catch loops on pieces of code to catch more specific types of exceptions? (I donโ€™t think because this is a new application, and using more specific catch of exceptions means that I have an idea of โ€‹โ€‹what will go wrong ... I do not do this, so it seems to me that makes sense.)

-Adeena

+6
c # exception exception-handling
source share
6 answers

I think you're right, you donโ€™t know what will go wrong.

However, you may also consider adding a handler to a ThreadException .

The above code will work, but there will be scenarios in which multithreading can be a problem with such code, since not all the code inside your Windows form program will be launched in the main thread of the Application.Run loop.

Here is a sample code from a related article:

 [STAThread] static void Main() { System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(ReportError); System.Windows.Forms.Application.Run(new MainForm()); } private static void ReportError(object sender, ThreadExceptionEventArgs e) { using (ReportErrorDialog errorDlg = new ReportErrorDialog(e.Exception)) { errorDlg.ShowDialog(); } } 

Additional documentation on MSDN .

At a minor point, using the ThreadException event also allows you to continue the loop of your main message if the exception is not fatal (for example, fault tolerance scenarios), while the try / catch approach may need to restart the main message loop, which can cause side effects.

+9
source share

From an implementation point of view, does it make sense to put a try / catch loop in my main program.cs file, where does the application work?

Of course and always.

You should use Try / Catch-Blocks wherever you do something critical, which may throw an exception.

Therefore, you cannot really adhere to the pattern for this, because now you have to when which exception will be raised. Otherwise, these are unhandled exceptions that could cause your program to crash.

But there are many exceptions that should not completely stop the application, exceptions that can simply be swallowed, as expected, and do not require the critical need to stop the application. An example of this is UnauthorizedAccessExceptions when moving or accessing data with your program.

You should try so that you try / Catch -Blocks as little as possible, and also use not too many of them due to performance.

Some of them use Try / Catch to control program execution. This should be avoided wherever possible, because the exception is caused by Expression, the number 1 performance killer.

+1
source share

Wrapping a capture attempt around the entire application will mean that the application will fail.

When you try and catch every method, it's hard to maintain.

Best practice is to use certain catch attempts around code units that will throw specific types of exceptions, such as FormatException, and leave the general exception handling event handlers at the application level.

 try { //Code that could error here } catch (FormatException ex) { //Code to tell user of their error //all other errors will be handled //by the global error handler } 

Experience will tell you what things can go wrong. Over time, you will notice that your application often throws IO words to exceptions related to file access, so you can catch them later and provide the user with additional information.

Global error handlers will catch the rest. You use them by connecting event handlers to two events. System.Windows.Forms.Application.ThreadException ( see MSDN ) and AppDomain.UnhandledException ( see MSDN )

Remember that memory exceptions and a StackOverflowException cannot be caught when errors are detected.

+1
source share

If you want to automatically receive stack traces, Microsoft allows you to send them through the error reporting services. All you have to do is register a digital certificate from VeriSign and register it (for free) with Microsoft.

Microsoft then gives you a login to download mini-dumps from the website, which are sent when the user clicks "Submit Error Report".

Although people can click "Do Not Submit", at least this is a Microsoft dialog box and maybe not the one that you need to code yourself. It will work 24/7, you do not have to worry about the operating time of your web server. And you can send detailed crawl details to users, and you can deliver updates through Windows Update.

Information about this service can be found in this article Windows Error Report: Getting Started .

+1
source share

The best approach is singing for AppDomain.UnhandledException and Application.ThreadException. In your application, the main function. This will allow you to record any unhandled exceptions in your application. A break in a try catch block does not catch everything.

0
source share

if you just want to capture failures, then ignore all the errors and let DrWatson create a mini drive for you. Then you can look at this ina debugger (windbg is preferable for mini-disks), and it will show you the line where your code entered incorrectly, as well as all the parameters, stack trace and registers. You can install Drwatson to create a complete dump, where you get the whole core dump that will be examined.

I would not recommend putting try / catch throughout the application, if you do not want your application to never โ€œbreakโ€ before the user - it will always be processed and will probably be ignored, since you cannot do anything with an exception at this point.

Sending minidump to you is another matter, but here is the article , you will need to do some work to send it via email / HTTP / FTP / etc.

0
source share

All Articles