PRB: Unhandled Exceptions in Windows Form Events Are Not Propagated Up Call Stack (324653)
The information in this article applies to:
- Microsoft Windows .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 "Steps to Reproduce the
Behavior" 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.
- 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("Handled the following exception:" & _
vbCrLf & vbCrLf & e.Exception.Message)
End Sub
End Class
- On the Debug menu, click Start Without Debugging.
- Click the command button. Notice that you receive the
following message:
Handled the following
exception:
My Exception
STATUSThis
behavior is by design.
Modification Type: | Major | Last Reviewed: | 6/24/2005 |
---|
Keywords: | kbNetFrame100preSP3fix kbprb KB324653 |
---|
|