473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Exception Handling in Windows Forms Application - UnhandledExceptionHandler

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
2 9503
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ritz, Bruno | last post by:
hi in java i found that when a method has a throws clause in the definition, callers must either handle the exceptions thrown by the method they are calling or "forward" the exception to the...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
5
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
7
by: ZorpiedoMan | last post by:
Well, I'm still wondering around in the "land of the lost exception"... It seems that controls that are bound to a class and the class throws an error in the SET method of the bound member, the...
1
by: Chris Dunaway | last post by:
I have a Windows forms app which is started from a sub Main. In Sub main I have this line: AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
6
by: Steve | last post by:
Hi All I have a windows forms Application (SAM) in vb.net 2008 using .net framework V2 One and only one customer out of 30 customers is getting errors daily where they have to close and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.