473,320 Members | 2,088 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,320 software developers and data experts.

Quirk with RadioButtons when Application.ThreadException is being used to handle Unexpected Exceptions

I was wondering if anyone else had come across this "feature" of .NET
and if they had any idea what might be causing it.

I've been writing my first C# Windows application (so if there's
anything so appalling in my code that it gives experienced developers a
touch of the vapours, I hope you'll be indulgent!) I've written my own
code to handle unexpected exceptions and I've added this as the handler
to Application.ThreadException. It all works fine - until my WinForm
has a radiobutton on it with its Checked property set to True at design
time, at which point the handler is no longer called. Switch the
checked property back to false, or set it to True at runtime, and
everything is fine again, and the custom handler captures the exception
and allows the application to exit gracefully.

I suspect this is a feature of the .NET framework rather than C# in
particular, which is why I've posted here. I've not been able to find
this mentioned anywhere else online, despite quite a few searches.

The following code will replicate the problem:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Steria.ExceptionManagementTemplate
{
/// <summary>
/// Summary description for Test.
/// </summary>
public class Test : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.GroupBox fraErrorType;
private System.Windows.Forms.RadioButton optDatabaseUnavailable;
private System.Windows.Forms.RadioButton optUnexpectedException;
public static Test AppForm;

public Test()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.fraErrorType = new System.Windows.Forms.GroupBox();
this.optUnexpectedException = new
System.Windows.Forms.RadioButton();
this.optDatabaseUnavailable = new
System.Windows.Forms.RadioButton();
this.fraErrorType.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(160, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Test";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// fraErrorType
//
this.fraErrorType.Controls.Add(this.optUnexpectedE xception);
this.fraErrorType.Controls.Add(this.optDatabaseUna vailable);
this.fraErrorType.Location = new System.Drawing.Point(8, 16);
this.fraErrorType.Name = "fraErrorType";
this.fraErrorType.Size = new System.Drawing.Size(256, 88);
this.fraErrorType.TabIndex = 17;
this.fraErrorType.TabStop = false;
//
// optUnexpectedException
//
this.optUnexpectedException.Location = new System.Drawing.Point(16,
24);
this.optUnexpectedException.Name = "optUnexpectedException";
this.optUnexpectedException.Size = new System.Drawing.Size(232, 16);
this.optUnexpectedException.TabIndex = 8;
this.optUnexpectedException.Text = "Unexpected Exception";
//
// optDatabaseUnavailable
//
this.optDatabaseUnavailable.Location = new System.Drawing.Point(16,
48);
this.optDatabaseUnavailable.Name = "optDatabaseUnavailable";
this.optDatabaseUnavailable.Size = new System.Drawing.Size(232, 16);
this.optDatabaseUnavailable.TabIndex = 7;
this.optDatabaseUnavailable.Text = "Database Unavailable";
//
// Test
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(280, 141);
this.Controls.Add(this.fraErrorType);
this.Controls.Add(this.button1);
this.Name = "Test";
this.Text = "Test";
this.fraErrorType.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
AppForm = new Test();

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(AppFo rm.GlobalExceptionHandler);
Application.Run(AppForm);
}

private void button1_Click(object sender, System.EventArgs e)
{
DivideByZeroException ex = new DivideByZeroException();

throw ex;
}

public void GlobalExceptionHandler (object source,
System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show ("GlobalExceptionHandler", "The
GlobalExceptionHandler has been called.");
}

}
}

Jan 19 '06 #1
2 1476
On 19 Jan 2006 06:19:08 -0800, pa********@bigfoot.com wrote:
I've been writing my first C# Windows application (so if there's
anything so appalling in my code that it gives experienced developers a
touch of the vapours, I hope you'll be indulgent!) I've written my own
code to handle unexpected exceptions and I've added this as the handler
to Application.ThreadException. It all works fine - until my WinForm
has a radiobutton on it with its Checked property set to True at design
time, at which point the handler is no longer called.


Same behaviour here but i was not that surprised: i've already noticed that
unhandled exception handlers behaved erratically when the porgram was run
from Visual Studio. Try to run your program outside Visual Studio (that is:
double click on the generated .exe file) and you'll find that your program
runs as excpected. This seems to me to be more like a Visual Studio bug
rather than a .NET Framework bug. Anyway, once in the field this bug never
seemed to appear so it's not that much of a problem.
Jan 19 '06 #2
You're absolutely right - I've just run the .exe and it worked
correctly. Visual Studio certainly looks like the culprit, then.

Thanks for the quick response!
Mehdi wrote:
On 19 Jan 2006 06:19:08 -0800, pa********@bigfoot.com wrote:
I've been writing my first C# Windows application (so if there's
anything so appalling in my code that it gives experienced developers a
touch of the vapours, I hope you'll be indulgent!) I've written my own
code to handle unexpected exceptions and I've added this as the handler
to Application.ThreadException. It all works fine - until my WinForm
has a radiobutton on it with its Checked property set to True at design
time, at which point the handler is no longer called.


Same behaviour here but i was not that surprised: i've already noticed that
unhandled exception handlers behaved erratically when the porgram was run
from Visual Studio. Try to run your program outside Visual Studio (that is:
double click on the generated .exe file) and you'll find that your program
runs as excpected. This seems to me to be more like a Visual Studio bug
rather than a .NET Framework bug. Anyway, once in the field this bug never
seemed to appear so it's not that much of a problem.


Jan 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: kelvSYC | last post by:
What I want to do is to read a 32-bit unsigned integer (let's call that u32) in little-endian form from an fstream. Would it be better if my function went like this: // returns false if an...
12
by: Rakesh | last post by:
Hi, I am catching unhandled exception by attached an event handler on the Application.ThreadException event. This will ensure that I could handle all uncaught exceptions on the main thread. ...
2
by: Mathieu Brüning | last post by:
Hi all, i'm trying to write an exception handler for my application, so in case an exception occurs anywhere in my application, the exception has to be catched. At this moment i'm doing it...
4
by: Al Williams | last post by:
Hi, I have error handling in place throughout my application. I also start the application wrapped in error handling code to catch any unexpected exceptions (i.e. exceptions that occur where I...
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...
0
by: Jon | last post by:
I have a reproducible problem. In my application I have handlers for both the AppDomain.CurrentDomain.UnhandledException event and the Applciation.ThreadException events. Up until now these...
4
by: Steve | last post by:
I have read a couple articles online, read my Jesse Liberty book but I am still confused as to just what the best practices are for using exceptions. I keep changing how I'm working with them and...
3
by: Shehab Kamal | last post by:
In Main I register ThreadException to handle unhandled exceptions Application.ThreadException += new ThreadExceptionEventHandler(App_ThreadException); The method will show a MessageBox or a Form...
3
by: CharlieC | last post by:
I am writing a windows C# application, and I want to ensure that all exceptions, handled or otherwise, are logged to the local event log. I can do this for the handled exceptions, but I am not sure...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.