M
Martin
This simple code has two button handlers, one throws an exception, the
other asynchronously throws an exception. Each thrown exception has an
outer and inner part, the first exception thrown shows a message box
that says 'ApplicationException: Useful message -->
DivideByZeroException: Attempted to divide by zero' and then a stack
trace, the asynchronous throw produces a messagebox which says
'DivideByZeroException: Attempted to divide by zero'. This begs the
obvious question, in the asynchronous throw what has happened to the
exception I threw, how do I get it back, and how do I write exception
safe code when I have to expect that any exception I throw will be
dismantled before I ever get a chance to handle it?
using System;
using System.Threading;
using System.Windows.Forms;
namespace ExceptionTest2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
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.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(96, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(96, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 48);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new
System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
Application.Run(new Form1());
}
private static void OnThreadException(object sender,
ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
private void button1_Click(object sender, System.EventArgs e)
{
int a = 0;
try
{
int b = 1 / a;
}
catch(System.DivideByZeroException ex)
{
throw new ApplicationException("Useful message", ex);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
BeginInvoke(new VoidDlg(AsyncThrow));
}
private void AsyncThrow()
{
int a = 0;
try
{
int b = 1 / a;
}
catch(System.DivideByZeroException ex)
{
throw new ApplicationException("Useful message", ex);
}
}
}
delegate void VoidDlg();
}
other asynchronously throws an exception. Each thrown exception has an
outer and inner part, the first exception thrown shows a message box
that says 'ApplicationException: Useful message -->
DivideByZeroException: Attempted to divide by zero' and then a stack
trace, the asynchronous throw produces a messagebox which says
'DivideByZeroException: Attempted to divide by zero'. This begs the
obvious question, in the asynchronous throw what has happened to the
exception I threw, how do I get it back, and how do I write exception
safe code when I have to expect that any exception I throw will be
dismantled before I ever get a chance to handle it?
using System;
using System.Threading;
using System.Windows.Forms;
namespace ExceptionTest2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
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.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(96, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new
System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(96, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 48);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new
System.EventHandler(this.button2_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.ThreadException += new
ThreadExceptionEventHandler(OnThreadException);
Application.Run(new Form1());
}
private static void OnThreadException(object sender,
ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
}
private void button1_Click(object sender, System.EventArgs e)
{
int a = 0;
try
{
int b = 1 / a;
}
catch(System.DivideByZeroException ex)
{
throw new ApplicationException("Useful message", ex);
}
}
private void button2_Click(object sender, System.EventArgs e)
{
BeginInvoke(new VoidDlg(AsyncThrow));
}
private void AsyncThrow()
{
int a = 0;
try
{
int b = 1 / a;
}
catch(System.DivideByZeroException ex)
{
throw new ApplicationException("Useful message", ex);
}
}
}
delegate void VoidDlg();
}