代码之家  ›  专栏  ›  技术社区  ›  Adam Kane

从IDE调试项目时捕获异常,但在IDE外部运行时未捕获异常

  •  3
  • Adam Kane  · 技术社区  · 14 年前

    在C#、WinForms、VS2008、.NET 3.5中。。。

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
    
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormThatDividesByZero());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    
        }
    }
    
    public partial class FormThatDividesByZero : Form
    {
        public FormThatDividesByZero()
        {
            InitializeComponent();
        }
    
        private void DivideByZeroButton_Click(object sender, EventArgs e)
        {
            // Create a divide by zero exception.
            int a = 0;
            int b = 0;
            int c = a / b;
        }
    }
    

    完整来源: http://forgefx.com/posts/ExceptionReporting.zip

    当我在开发环境中通过F5运行这个小测试项目时,单击DivideByZero按钮后的异常被捕获,并触发消息框。当我通过双击/bin/Debug文件夹中的.exe来运行项目时,不会捕获异常并且没有消息框-为什么会这样?

    从IDE外部启动.exe时,或使用“Debug>在visualstudio中,我得到一个未处理的异常。我的理解是上面的代码将捕获所有异常。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Aik    14 年前

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
    
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormThatDividesByZero());            
    
        }
    
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            MessageDialog.Show(ex.Message);
            Application.Exit();
        }
    
        void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageDialog.Show(e.Exception.Message);
        }
    }
    
    
    
    public partial class FormThatDividesByZero : Form
    {
        public FormThatDividesByZero()
        {
            InitializeComponent();
        }
    
        private void DivideByZeroButton_Click(object sender, EventArgs e)
        {
            // Create a divide by zero exception.
            int a = 0;
            int b = 0;
            int c = a / b;
        }
    }
    
        2
  •  0
  •   John Pickup    14 年前

    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

    void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageBox.Show("ThreadException:"+e.Exception.Message);
    }
    

    通过这种方式,您可以处理任何事件代码中未捕获的异常,而无需结束应用程序,例如通过日志记录或警报。当然,在发生这种意外的异常之后,应用程序是否仍然稳定还需要考虑,即应用程序继续运行是否有意义?