By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Exception Handling in Windows Forms Application - UnhandledExceptionHandler

P: n/a
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(UnhandledExceptionH andler);

// 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");
}
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ba*******@yahoo.com> wrote in message
news:ce*************************@posting.google.co m...
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(UnhandledExceptionH andler);

// 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");
}


Nov 15 '05 #2

P: n/a
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote in message news:<eX*************@TK2MSFTNGP10.phx.gbl>...
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" <ba*******@yahoo.com> wrote in message
news:ce*************************@posting.google.co m...
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(UnhandledExceptionH andler);

// 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");
}

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.