Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 4-Aug-11 4:33am
Rate this: bad
good
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.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
  Permalink  
v2
Comments
prathameshpitale at 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 at 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 at 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
good
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.
  Permalink  
Comments
prathameshpitale at 4-Aug-11 11:48am
   
Can you give me Example or Link of example?
SAKryukov at 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
Shameel at 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
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 7 Aug 2011
Copyright © CodeProject, 1999-2014
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