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.
You can catch all exceptions on AppDoamin Level by signing for UnhandledException event
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With