Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect windows errors/popups

My app doesn't cover all the exception scenarios. During run time, sometimes a window pops up which shows the error for ex: "System Null reference exception" or "The file or directory is corrupted and unreadable". Once the error window comes up, it doesn't go away until the user responds with the buttons.

I want to capture these exception and don't want to show these error windows to the users.

like image 971
user209293 Avatar asked May 16 '11 15:05

user209293


3 Answers

You can catch all exceptions on AppDoamin Level by signing for UnhandledException event

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
like image 144
Stecya Avatar answered Oct 09 '22 16:10

Stecya


You need to wrap your code in a try {} catch(Exception e) {} block so you can catch the error yourself.

example:

try
{
  // all of my code
}
catch ( Exception e )
{
  // show my own error dialog
}
like image 34
Michael Pryor Avatar answered Oct 09 '22 16:10

Michael Pryor


It sounds like you need to go back through your code and look at your exception handling. Either there are sections of code that are not inside of a try/catch block or you have some try/catch blocks that do not handle all of the possible exceptions.

You need to make sure that when you do wrap these sections of code with try/catch blocks that you don't just eat the errors but instead log them. You don't want your application having unexpected errors without something happening because those unexpected errors can leave your application in a vulnerable state. Sometimes it is better to let your application crash than it is to hide these errors.

Here is a good article on using the try/catch:

http://msdn.microsoft.com/en-us/library/ms173160.aspx

When you implement try/catch, make sure you also consider putting a finally in to clean up any connection information or other code that should be closed out. It would look like so:

try
{
   //Your existing code
}
catch (Exception ex)
{
   //Here is where you log the error - ex contains your entire exception so use that in the log
}
finally
{
   //Clean up any open connections, etc. here
}

Notice that the catch block catches the generic Exception so all errors that aren't caught above (in more specific catch blocks) should be caught here.

like image 1
IAmTimCorey Avatar answered Oct 09 '22 15:10

IAmTimCorey