为什么.NET4 try-catch在Release中的简单用户异常上失败?

时间:2011-11-05 14:54:23

标签: .net-4.0

这是一个例子。在下面的应用程序中,我单击Start,然后单击Stop - in Release only *,Start例程的catch不会获得Stop的异常。

*编辑:现在我发现这不是发布版本,而是发送到调试器外部的任何版本。即调试器外部的Release和Debug构建失败,调试器内的Debug构建正常。相当担心看到没有连接调试器会产生这样的不利影响!

编辑:这是从Windows资源管理器运行发布版本的堆栈跟踪:

WindowsFormsApplication1.RunStopException: Exception of type 'WindowsFormsApplication1.RunStopException' was thrown.
at WindowsFormsApplication1.Form1.Stop_button_Click(Object sender, EventArgs e) in D:\Projects\! FastSpecc FAP2\RunStopExceptionfail\Form1.cs:line 35
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

以及从VS2010调试器运行Debug的堆栈跟踪:http://img502.imageshack.us/img502/2749/runstopexceptionfaildeb.png

enter image description here

代码为文字:

    using System;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Start_button.Enabled = true; Stop_button.Enabled = false;
            }
            private void Start_button_Click(object sender, EventArgs e)
            {
                this.Text= "Started";
                try
                {
                    Start_button.Enabled = false; Stop_button.Enabled = true;
                    while (true)
                        Application.DoEvents();
                }
                catch (RunStopException)
                {
                    // in Debug build, works fine.
                    // In Release build, fails
                    //  Unhandled exception has occurred in your application. ...
                    //  Exception of type 'WindowsFormsApplication1.RunStopException' was thrown.
                    this.Text= "Stopped";
                    Start_button.Enabled = true; Stop_button.Enabled = false;
                }
            }
            private void Stop_button_Click(object sender, EventArgs e)
            {
                this.Text=  "Stopping";
                throw new RunStopException();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
        }
        public class RunStopException : Exception { }
    }

0 个答案:

没有答案