c# – 具有多种形式的异常处理

当我正在调试时,当我运行一个编译的.exe时,我会看到不同的行为,例外被捕获或被抓住.我有两种形式(Form1和Form2). Form1上有一个按钮,它在Form2上实例化并调用ShowDialog. Form2有一个按钮,它有意地产生一个除零的错误.当我调试时,Form1中的catch块被击中.当我运行编译的.exe,它没有被打击,而是我收到一个消息框,指出“您的应用程序中发生了未处理的异常.如果您单击继续,应用程序将忽略此错误并尝试继续.点击退出,应用程序将立即关闭…尝试除以零“.我的问题是为什么在调试时和运行.exe时会有不同的行为?如果这是预期的行为,那么认为有必要在每一个事件处理程序中放置try / catch块?看起来有点疯狂杀了,不是吗?

这是Form1的代码.

public partial class Form1 : Form
{
    public Form1()
    {
            InitializeComponent();

    }

    private void button1_Click(object sender,EventArgs e)
    {
        try
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
        catch(Exception eX)
        {
            MessageBox.Show( eX.ToString()); //This line hit when debugging only
        }
    }
}

这是Form2的代码

public partial class Form2 : Form
{
    public Form2()
    {
            InitializeComponent();
    }

    private void button1_Click(object sender,EventArgs e)
    {
            int x = 0;
            int y = 7 / x;

    }
}

解决方法

我得到和你一样的行为.我不知道为什么会发生这种情况,但是假设从表单中的事件生成的异常将出现在ShowDialog()调用的堆栈上似乎是一个坏主意.做这两件事情会更好

>抓住并处理Form2中的事件处理程序中的异常,这样做是有意义的,当您可以对异常进行有意义的事情时.
>为整个应用程序添加一个未处理的异常处理程序(“Application_ThreadException”),以捕获任何未处理的异常.

更新:这是堆栈跟踪.调试版本:

System.DivideByZeroException: Attempted to divide by zero.
   at WindowsFormsApplication1.Form2.button1_Click(Object sender,EventArgs e) in ...\WindowsFormsApplication1\Form2.cs:line 27
   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.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.dispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.imsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 reason,Int32 pvLoopData)
   at System.Windows.Forms.Application.threadcontext.RunMessageLoopInner(Int32 reason,ApplicationContext context)
   at System.Windows.Forms.Application.threadcontext.RunMessageLoop(Int32 reason,ApplicationContext context)
   at System.Windows.Forms.Application.RunDialog(Form form)
   at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
   at System.Windows.Forms.Form.ShowDialog()
   at WindowsFormsApplication1.Form1.button1_Click(Object sender,EventArgs e) in ...\WindowsFormsApplication1\Form1.cs:line 45

发布:

System.DivideByZeroException: Attempted to divide by zero.
   at WindowsFormsApplication1.Form2.button1_Click(Object sender,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,IntPtr lparam)

请注意,System.Windows.Forms.Form.ShowDialog()不是在释放模式下的堆栈跟踪,这就是为什么你的try {} catch {}什么都不做.另外值得注意的是,在调试的情况下,它使用的是NativeWindow.DebuggableCallback,大概是通过不打破堆栈来帮助调试,而在Release模式下使用NativeWindow.Callback.

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么