Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
My Application is Designed in c#.net 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?


Prathamesh
Posted

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

C#
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

//...

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


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

—SA
 
Share this answer
 
v2
Comments
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,
Sergey Alexandrovich Kryukov 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 :-)
--SA
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 ?
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.
 
Share this answer
 
Comments
prathameshpitale 4-Aug-11 11:48am    
Can you give me Example or Link of example?
Sergey Alexandrovich Kryukov 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.
--SA
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900