views:

112

answers:

3

Hi,

I'm developing a GUI app in c#.

Its a multithread app, and I would like to wrap all the threads (some of them I don't open e.g. NetClient.StartDownload which is none blocking function) with a try/catch . Statment so that if an exception is thrown an uncought, I could log it and report to base.

I tried using Application.ThreadException and AppDomain.CurrentDomain.UnhandledException, they seam to only catch GUI exceptions.

Please help.

Thanks.

+4  A: 

The code in each thread would need to have a try-catch. Exceptions are not marshalled across threads... and an unhandled exception just brings the application down.

To catch all Exceptions - just use the base Exception type.

catch(Exception e) { // log e } 

Updated: You could take a look at AppDomain.UnhandledException - handle this event for logging unhandled exceptions on any thread ; however you can't stop the application from going down. For more check out http://www.albahari.com/threading/#_Introduction - towards the end of that page.

Gishu
@0xA3 - corrected.. thanks.
Gishu
A: 

We need to catch exceptions at the smallest possible unit. As mentioned by Gishu, Exceptions which occur in the threads do not get propgated back to the main thread in many cases.

I had blogged about a similar experience some time back at WCF service unhandled exception error

Nilesh Gule
A: 

The best answer is: don't. There's already a system for doing this built right into the operating system.

If you let the exceptions "bubble up" and out of the program, modern OS'es include Windows Error Reporting, which will give the user the option of sending an error report to Microsoft. (Note: do not send error reports automatically - think long and hard about the legal ramifications regarding privacy laws...)

The error report includes not just the exception information and full stack trace, but also includes a good portion of your process' memory, as well as exactly which OS modules were loaded (so you can even tell whether the client had a relevant Windows Update patch applied). It wraps up all of this information into a minidump file (and also includes a few XML files with additional info).

This error report is uploaded to Microsoft's servers, so you don't have to worry about setting up and maintaining a "call home" server. All error reports for your programs are categorized using advanced heuristics into "buckets", with each "bucket" containing reports that are likely to be caused by the same bug.

Microsoft exposes all of this information to you (as the software publisher) through a site called Winqual. Using Winqual, you can examine all of the error reports, and use their heuristical bucketizing algorithms to decide which bugs are most impacting your customers. You can also download each individual report for more detailed investigation.

If you have a symbol server and source control server set up in your organization (and you certainly should), then you can just load the minidump from a downloaded error report right into Visual Studio, and it will automagically check the old source out of source control and allow you to inspect the exact state of the program at the moment it crashed.

Finding and fixing bugs has never been this easy.

To summarize, here's what you need to do:

  1. Set up a symbol server and a source server in your organization. Establish a release policy to ensure that all releases get source-indexed pdbs added to the symbol server before they get released to the customer.
  2. Establish an account with Winqual. You'll need an Authenticode code-signing certificate; if you get a non-VeriSign code-signing cert, then you'll also have to spend $100 for an "organizational certificate" from VeriSign.
  3. Modify your release policy to include creating mapping files for your releases. These mapping files are uploaded to Winqual before shipping the release.
  4. Do not catch unexpected exceptions. If you do ever need to catch them, be sure to rethrow using throw and not throw ex, so the stack trace and original program state is preserved.

For more information, I can highly recommend Debugging .NET 2.0 Applications by John Robbins. In spite of the "2.0" in the title, this book is still completely relevant today (except that the only source server example is using Visual SourceSafe, which was a complete joke even back then). Another good book if you need to do a lot of debugging is Advanced .NET Debugging, though it is starting to show its age, especially since the new VS2010 debugging advancements make a lot of its techniques out of date.

Stephen Cleary