An exception does not propagate correctly to the calling function in a Windows Forms application project in Visual Studio .NET (836674)
The information in this article applies to:
- Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2003), Academic Edition
- Microsoft Visual Studio .NET (2002), Professional Edition
- Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
- Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
- Microsoft Visual Studio .NET (2002), Academic Edition
SYMPTOMSWhen you use structured exception handling in a Microsoft
Windows Forms application, the exception may not propagate correctly to the calling
function. This behavior may occur if you run your Windows Forms application without
using the debugger. When this behavior occurs, you may receive the following error message: An unhandled exception has occurred in your application. If you
click continue, the application will ignore this error and attempt to continue.
If you click Quit the application will shut down immediately. My Exception
However, if you run your Windows Forms application with the debugger,
you may not receive this error message. CAUSEWhen you run your Windows Forms application without using
the debugger, you use the NativeWindow.CallBack method to catch the exception and to prevent the program from
unexpectedly quitting (crashing). In the NativeWindow.CallBack method, you populate the exception message by using a standard
exception dialog box.
However, if you run your Windows Forms
application with the debugger, you do not catch the exception because you use
the NativeWindow.DebuggableCallBack method. When you use the NativeWindow.DebuggableCallBack method, the just-in-time (JIT) debugger stops the application
from running.WORKAROUNDTo work around this behavior, follow these steps:
- In Microsoft Visual Studio .NET, open the Windows Forms
project.
- Click Solution Explorer.
- In Solution Explorer, right-click the project, point to
Add, and then click Add New Item. The
New Item dialog box appears.
- Do one of the following, depending on the version of Visual
Studio .NET that you have:
- In Visual Studio .NET 2003, click Application
Configuration File under Template, and then click
Open.
- In Visual Studio .NET 2002, click XML
File under Template, type
App.config in the Name box, and then
click Open.
The App.config file opens in the Visual Studio .NET
IDE. - In the App.config file, replace the existing code with the
following code.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration> - On the Build menu, click Build
Solution.
- Press CTRL+F5 to run the application without using the
debugger.
Another workaround for this behavior is to use the Application.OnThreadException handler. Add the following code to the Form1 class. This code works the same way both with debugger and
without the debugger. [STAThread]
static void Main()
{
CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
Application.Run(new Form1());
}
internal class CustomExceptionHandler
{
public void OnThreadException(object sender, ThreadExceptionEventArgs t)
{
// MessageBox.Show("OnThreadException - Handling the following exception: \n \n" + t.Exception.Message);
throw t.Exception;
}
} STATUS
This behavior is by design.
Note This behavior does not occur in Microsoft Visual Studio 2005.
Modification Type: | Major | Last Reviewed: | 2/24/2006 |
---|
Keywords: | kbvs2005swept kbvs2005doesnotapply kbHotfixServer kbQFE kbvs2002sp1sweep kberrmsg kbpending kbExceptHandling kbProgramming kbDebug kbAppDev kbbug KB836674 kbAudDeveloper |
---|
|