Exception Handling in Windows Forms Application - UnhandledExceptionHandler

S

Steph

I am working on a Windows Forms App. I have attached a standard
UnhandledExceptionEventHandler to the current domain to catch
unexpected errors. In my application, when I trigger an unhandled
error, my handler does not get hit - I get the standard ugly run time
error. The strange thing is, I tried the exact same code in a new,
windows forms application, and when I trigger an unhandled exception,
the code works fine (the JIT compiler dialog box comes up, then my
exception handler runs).

The only thing different about the two applications is that the form
in the application that is not working is in a MDI child form. I have
pasted the same code, however, into the Main() of the parent form and
triggered an error there, with the same result, the code does not get
hit.

Are there some VID settings that I am missing that could be varying
between the applications to cause this behavior?

The code snippet that is misbehaving is as follows:

// in the form constructor
AppDomain adCurrent = AppDomain.CurrentDomain;
adCurrent.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledExceptionHandler);

// in the form constructor to trigger the error
decimal dec = Convert.ToDecimal("apple");

private static void UnhandledExceptionHandler(object sender,
UnhandledExceptionEventArgs ue)
{
Exception unhandledException = (Exception) ue.ExceptionObject;

if (!EventLog.SourceExists("Intake"))
{
EventLog.CreateEventSource("Intake","Intake Log");
}

EventLog eventLog = new EventLog();
eventLog.Source = "Intake";
eventLog.WriteEntry(unhandledException.Message);
MessageBox.Show("unhandled exception");
}
 
D

Dmitriy Lapshin [C# / .NET MVP]

Steph,

Try using the Application.ThreadException instead for WinForms applications.

P.S. I would like to invite Jay Harlow to this thread, we've recently
discussed the use of these events to react on unhandled exceptionds.
 
S

Steph

Interesting. Dmitriy - I tried using Application.ThreadException in
place of the UnhandledExceptionEventHandler I was using before. In
this case, instead of getting the run time error, the development
environment dialog box comes up with the error (this is the JIT
compiler I believe?), but when I choose "continue" the
ThreadExceptionEventHandler I attached does not get hit at all.
Again, what I find interesting is that the
UnhandledExceptionEventHandler that I tried initially worked just fine
in a _different_ project. This makes me think that there either has
to be user error on my part or some difference in the project settings
that is affecting how which error handler gets hit. Am I missing some
documentation from MSoft or are all the kinks not worked out yet in
the framework's error handling mechanisms?

Dmitriy Lapshin said:
Steph,

Try using the Application.ThreadException instead for WinForms applications.

P.S. I would like to invite Jay Harlow to this thread, we've recently
discussed the use of these events to react on unhandled exceptionds.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Steph said:
I am working on a Windows Forms App. I have attached a standard
UnhandledExceptionEventHandler to the current domain to catch
unexpected errors. In my application, when I trigger an unhandled
error, my handler does not get hit - I get the standard ugly run time
error. The strange thing is, I tried the exact same code in a new,
windows forms application, and when I trigger an unhandled exception,
the code works fine (the JIT compiler dialog box comes up, then my
exception handler runs).

The only thing different about the two applications is that the form
in the application that is not working is in a MDI child form. I have
pasted the same code, however, into the Main() of the parent form and
triggered an error there, with the same result, the code does not get
hit.

Are there some VID settings that I am missing that could be varying
between the applications to cause this behavior?

The code snippet that is misbehaving is as follows:

// in the form constructor
AppDomain adCurrent = AppDomain.CurrentDomain;
adCurrent.UnhandledException += new
UnhandledExceptionEventHandler(UnhandledExceptionHandler);

// in the form constructor to trigger the error
decimal dec = Convert.ToDecimal("apple");

private static void UnhandledExceptionHandler(object sender,
UnhandledExceptionEventArgs ue)
{
Exception unhandledException = (Exception) ue.ExceptionObject;

if (!EventLog.SourceExists("Intake"))
{
EventLog.CreateEventSource("Intake","Intake Log");
}

EventLog eventLog = new EventLog();
eventLog.Source = "Intake";
eventLog.WriteEntry(unhandledException.Message);
MessageBox.Show("unhandled exception");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top