Microsoft KB Archive/324653

= PRB: Unhandled Exceptions in Windows Form Events Are Not Propagated Up Call Stack =

Article ID: 324653

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-



This article was previously published under Q324653



SYMPTOMS
Unhandled exceptions that occur in Windows Form events are not propagated up the call stack to a structured exception handler in the calling procedure.



CAUSE
The Microsoft .NET Framework wraps the message pump in an exception handler. The .NET Framework only handles exceptions that reach the message pump and that the application has not already handled. Therefore, any unhandled exception that reaches the message pump is not propagated up the call stack.



RESOLUTION
To resolve this problem, create an exception handler for your unhandled exceptions by calling the Application.AddOnThreadException method and by passing the Application.ThreadException method a reference to a method in your application. This method is then called for any unhandled exceptions that occur on that thread. The OnThreadException event is raised when the Windows Forms window procedure receives an exception.

The following steps and sample code demonstrate how to use the Application.AddOnThreadException method to handle these exceptions.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.  Repeat steps 1 through 7 in the &quot;Steps to Reproduce the Behavior&quot; section. View the code for the Form1 class. Expand the Windows Form Designer generated code region.  Replace the Sub New constructor with the following code: Public Sub New MyBase.New

'This call is required by the Windows Form Designer. InitializeComponent

'Add any initialization after the InitializeComponent call.

'Explicitly set apartment state to single-threaded apartment (STA). System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA Dim eh As New ExceptionHandler AddHandler Application.ThreadException, AddressOf eh.OnThreadException End Sub  On the Project menu, click Add Class. In the Name box, type ExceptionHandler.vb, and then click Open.</li>  Replace the code in the new ExceptionHandler class with the following code: Friend Class ExceptionHandler Public Sub OnThreadException(ByVal Sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)

'Add custom exception handling code here.

MessageBox.Show(&quot;Handled the following exception:&quot; & _           vbCrLf & vbCrLf & e.Exception.Message) End Sub End Class </li> On the Debug menu, click Start Without Debugging.</li> Click the command button. Notice that you receive the following message:

Handled the following exception:

My Exception

</li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Follow these steps to create a new Windows Application project in Visual Basic .NET: <ol style="list-style-type: lower-alpha;"> Start Microsoft Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> In the New Project dialog box, click Visual Basic Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.</li></ol> </li> Drag a Button control from the toolbox to Form1.</li> On the View menu, click Code to view the form class module.</li>  Add the following code to the Button1_Click event handler: Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 Try f.Show Catch exc As Exception MessageBox.Show(exc.Message) Finally f.Close End Try End Sub </li> On the Project menu, click Add Windows Form. Click OK to accept the default name (Form2) for the new form.</li> On the View menu, click Code.</li>  Add the following code to the Form2_Load event handler: Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Throw New System.Exception(&quot;My Exception&quot;) End Sub </li> On the Debug menu, click Start Without Debugging.</li> Click the command button. Notice that you receive the following error message:

An unhandled exception has occurred in your application. If you Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will be shut down immediately.

My Exception.

</li></ol>

This behavior appears to be incorrect because there is a structured exception handler in the calling procedure in Form1. However, because the call stack contains a message pump, the exception is not propagated up the call stack, and the exception handler in Form1 does not handle the exception. If you start the application with the debugger attached, the exception is handled. When the debugger is attached to the process, the exception is handled correctly because there are two message pumps that alllow the message to propagate up the callstack.

Additional query words: callstack

Keywords: kbnetframe100presp3fix kbprb KB324653

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.