Click here to Skip to main content
13,053,844 members (58,097 online)
Rate this:
Please Sign up or sign in to vote.
My Application is Designed in 3.5,

I want to Handle Unhandled Exceptions occured while running the application!
How can i do this? I Googled this Question but cant find answer!

How can i collect the Log of Errors, As my Application is Client Server, I need to Collect ALl the Errors on the centralised Place.

Also I need a screen shot of the Form when Erros occur, can you suggest any Idea When should i capture when error Occurs?

Posted 4-Aug-11 3:33am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Here is a code sample for you, as it's really good to know:

Application.ThreadException += delegate(object sender, System.Threading.ThreadExceptionEventArgs e) {
       string.Format("{0}:\n{1}", e.Exception.GetType().Name, e.Message),
       string.Format(" {0}: Exception", Application.ProductName),
}; //Application.ThreadException

I usually call first line from application entry point (Main), and the rest of it — form a constructor of a main form.

prathameshpitale 8-Aug-11 1:20am
Thank you, But can you tell me How can i have A Screenshot for the Current form where Excepetion has been Generated,
SAKryukov 8-Aug-11 1:25am
I don't understand; probably it needs some explanation of the problem. It depends on exception. Was taking a screenshot failed, or writing a file, or something else? If it failed it failed, isn't it? The user gets exception information. If file was not created due to permission problem, the user can repeat the operation and save it in proper location, but if the reason is your bug, the only solution is fixing it :-)
prathameshpitale 8-Aug-11 9:21am
hi SAKryukov,
I am using this code, and it works fine. But when i Remove CLR Headers and Offuscate the code, it Produces error, cannot change Thread Exception mode once any control is created on the Thread, WHat should i do ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can handle the Application.ThreadException event to handle all unhandled exceptions that are thrown in the primary application thread. To catch exceptions that occur in threads not created and owned by Windows Forms, use the AppDomain.UnhandledException.
prathameshpitale 4-Aug-11 11:48am
Can you give me Example or Link of example?
SAKryukov 7-Aug-11 1:23am
Correct, but it needs one more step: Application.SetUnhandledExceptionMode, so my 4.

Also, OP's request for the code sample is reasonable here -- I provided it, please see.
Shameel 7-Aug-11 9:52am
Thanks for the correction, I somehow missed that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 7 Aug 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100