9

I'm writing a little library which catches all unhandled exceptions, shows a little dialog (similar to the usual dialog of the NF) which gives the user the opportunity to send the exception to the developer. To do this, I use the UnhandledException-Event of the AppDomain like this:

app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
            ExceptionHandler handler = new ExceptionHandler((Exception)e.ExceptionObject, ExEntry);
            UnhandledExceptionListened(handler);
            if (Properties.Settings.Default.ShowStandardExceptionDialog)
            {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
            }
        };

ExceptionHandler and ExEntry are Classes of my Library. But: If an Exception occurs, the compiler jumps into my Lambda-Expression, tries to debug the first line of code and then shows the error which occurred before without working off the rest of the lambda. But if I just write:

 app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) =>
        {
                ExceptionDialog exdialog = new ExceptionDialog(handler);
                exdialog.ShowDialog();
        };

it works perfectly. Has anyone an idea why this doesn't work?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Reignbeaux
  • 1,333
  • 3
  • 12
  • 15
  • 1
    Wrap you top code in a try catch and put a watch point in the catch. Is there an exception and if so what is it ? – TheKingDave May 23 '13 at 15:37
  • What about simply testing the creation of your Exception Handler in a test unit? what happens than? – Eyal H May 23 '13 at 15:37

2 Answers2

5

There might be two reasons.

One is you did not set UnhandledExceptionMode properly:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

The other is you did not handle the ThreadException, and the thrown exception was not an unhandled exception but a thread exception.

The following is an example, you would need to modify it according to your scenario:

Application.ThreadException+=
    new ThreadExceptionEventHandler(Log.WriteThreadException);

AppDomain.CurrentDomain.UnhandledException+=
    new UnhandledExceptionEventHandler(Log.WriteUnhandledException);

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
2

In my experience, this doesn't work:

  public partial class Form1 : Form
  {

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1Load(object sender, EventArgs e)
    {
      Application.ThreadException += ApplicationThreadException;
      Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
      AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
    }

    void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
    {
      //throw new NotImplementedException();
    }

    void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      //throw new NotImplementedException();
    }
  }

But this does:

[STAThread]
static void Main()
{

  Application.ThreadException += ApplicationThreadException;
  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
}

static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
  //throw new NotImplementedException();
}

static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
  //throw new NotImplementedException();
}

Maybe it's necessary to setup the global handler prior to Application.Run()

bvj
  • 3,294
  • 31
  • 30