1

I have reports of users getting the Windows XP error "[My Application] encountered an error and needs to close" message. This message is an indication of an unhandled exception in my application right? I have AppDomain.Unhandlled exception wired up to handle any uncaught exceptions from my code so I'm not sure how users are receiving this error. An answer to a similar question said something about separate threads causing this, but those still run in the same app domain and so they would be handled wouldn't they? Here's my handling code just in case you spot a place where it is throwing an exception:

public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        #region Global Exception Handling
        string support = String.Empty;

        //try to get the support information from the common project
        try
        {
            support = Common.Constants.ErrorMessages.RETRY_OR_CONTACT_SUPPORT;
        }
        catch(Exception)
        {
            //Use hard coded support info if the common project is not accessible
            support = "Please try again or contact the Support Center at 877-555-5555";
        }

        string message = "An error occured that OASIS was not able to recover from. " + support;

        try
        {
            //Try to use the OasisErrorDialog to show the error. If the exception derives from Exception, use OasisErrorDialog to log it.
            if (e.ExceptionObject is Exception)
                OasisErrorDialog.ShowErrorDialog(message, "A Serious Error Has Occured", true, true, (Exception)e.ExceptionObject);
            else
            {
                //The exception doesn't derive from Exception so we need to try to log it using StepUp
                OasisErrorDialog.ShowErrorDialog(message, "A Serious Error Has Occured");
                ExceptionHandler.HandleException("An unhandled error that does not derive from the Exception class was thrown." + e.ExceptionObject.ToString());
            }
        }
        catch (Exception)
        {
            //The OasisErrorDialog wasn't available so display the error in a standard MessageBox
            MessageBox.Show(message, "A Serious Error Has Occured");

            try
            {
                //If the StepUp framework is still working, log the exception info
                if (e.ExceptionObject is Exception)
                    ExceptionHandler.HandleException("An unhandled exception occured", (Exception)e.ExceptionObject);
                else
                    ExceptionHandler.HandleException("An unhandled error occured: "+e.ExceptionObject.ToString());
            }
            catch (Exception) { }
        }
        #endregion
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
xr280xr
  • 12,621
  • 7
  • 81
  • 125
  • Hmm, the code doesn't call Environment.Exit() so this is normal. – Hans Passant Apr 04 '12 at 17:41
  • Would Environment.Exit() commonly be called in this context? I thought catching the exception would prevent it from raising any further. Though I did intend for the application to stop running at that point. – xr280xr Apr 11 '12 at 18:58

1 Answers1

0

If your application does something really horrible (e.g. faulty interop causes memory corruption) then you won't see an exception. Your application will just fail.

Slightly less horrible things such as running out of memory can also cause your application to fail without running your exception handler.

This question and its answers discuss the impossibility of catching all exceptions.

Community
  • 1
  • 1
arx
  • 16,686
  • 2
  • 44
  • 61
  • Thank you. So a "normal" (for lack of a better classification) type of .net exception should pass through the handler regardless of the thead it occurred on right? The code being used when the user reports the crash is littered with exception handling so I'm not sure what's happening at the moment. I know there is db connectivity issues going on and there are some unrelated background threads running which is why I ask. – xr280xr Feb 08 '12 at 18:46
  • As long as you only have one application domain, you should see all "normal" exceptions. – arx Feb 08 '12 at 18:58