473,854 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception Handling in Windows Forms Application - UnhandledExcept ionHandler

I am working on a Windows Forms App. I have attached a standard
UnhandledExcept ionEventHandler 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.Curre ntDomain;
adCurrent.Unhan dledException += new
UnhandledExcept ionEventHandler (UnhandledExcep tionHandler);

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

private static void UnhandledExcept ionHandler(obje ct sender,
UnhandledExcept ionEventArgs ue)
{
Exception unhandledExcept ion = (Exception) ue.ExceptionObj ect;

if (!EventLog.Sour ceExists("Intak e"))
{
EventLog.Create EventSource("In take","Intake Log");
}

EventLog eventLog = new EventLog();
eventLog.Source = "Intake";
eventLog.WriteE ntry(unhandledE xception.Messag e);
MessageBox.Show ("unhandled exception");
}
Nov 15 '05 #1
2 9573
Steph,

Try using the Application.Thr eadException 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*******@yaho o.com> wrote in message
news:ce******** *************** **@posting.goog le.com...
I am working on a Windows Forms App. I have attached a standard
UnhandledExcept ionEventHandler 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.Curre ntDomain;
adCurrent.Unhan dledException += new
UnhandledExcept ionEventHandler (UnhandledExcep tionHandler);

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

private static void UnhandledExcept ionHandler(obje ct sender,
UnhandledExcept ionEventArgs ue)
{
Exception unhandledExcept ion = (Exception) ue.ExceptionObj ect;

if (!EventLog.Sour ceExists("Intak e"))
{
EventLog.Create EventSource("In take","Intake Log");
}

EventLog eventLog = new EventLog();
eventLog.Source = "Intake";
eventLog.WriteE ntry(unhandledE xception.Messag e);
MessageBox.Show ("unhandled exception");
}


Nov 15 '05 #2
Interesting. Dmitriy - I tried using Application.Thr eadException in
place of the UnhandledExcept ionEventHandler 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
ThreadException EventHandler I attached does not get hit at all.
Again, what I find interesting is that the
UnhandledExcept ionEventHandler 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.c om> wrote in message news:<eX******* ******@TK2MSFTN GP10.phx.gbl>.. .
Steph,

Try using the Application.Thr eadException 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*******@yaho o.com> wrote in message
news:ce******** *************** **@posting.goog le.com...
I am working on a Windows Forms App. I have attached a standard
UnhandledExcept ionEventHandler 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.Curre ntDomain;
adCurrent.Unhan dledException += new
UnhandledExcept ionEventHandler (UnhandledExcep tionHandler);

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

private static void UnhandledExcept ionHandler(obje ct sender,
UnhandledExcept ionEventArgs ue)
{
Exception unhandledExcept ion = (Exception) ue.ExceptionObj ect;

if (!EventLog.Sour ceExists("Intak e"))
{
EventLog.Create EventSource("In take","Intake Log");
}

EventLog eventLog = new EventLog();
eventLog.Source = "Intake";
eventLog.WriteE ntry(unhandledE xception.Messag e);
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
3704
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 caller by specifying a throws clause as well. is there a similar machanism in c++? i want to force a developer to write handlers for all possible exceptions a method of my class library can throw.
7
6012
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
44
4242
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
5
5758
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 thread.Abort()) is reported twice: once in the debug window, and once through the message box. Is it because the thread was sleeping when the exception occurred?
4
2047
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 like this: Try TryToDoIt() Catch e as Exception LogTheError(e)
7
1714
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 exception cannot be caught. I have wrapped my entire application in all the global exception handlers I can find, and still to no avail. In fact all the Try/Catches and Exception event handlers are worthless... Put on your advanced thinking...
1
1766
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 Form1.UnhandledExceptionHandler And I also have the method, UnhandledExceptionHandler with the proper signature. The handler logs details of the exception to a log file as well as sending an EMail. When I run the program through the IDE, exceptions are caught by this...
11
5610
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
4403
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 restart my application, sometimes several times a day The customer has XP Home SP2
0
9901
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9751
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10756
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5941
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3186
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.