建了两个窗体,form1用于显示异常结果form1代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2(Exception m_Error)
        {
            InitializeComponent();
            e = m_Error;
            label1.Text += e.Message;
            label2.Text += e.HelpLink;
            label3.Text += e.Source;
            textBox1.Text += e.StackTrace;
            textBox2.Text += e.TargetSite.ToString();            label4.Text += Environment.CurrentDirectory;
            label5.Text += Environment.MachineName;
            label6.Text += Environment.OSVersion;
            label7.Text += Environment.SystemDirectory;
            label8.Text += Environment.UserName;
            label9.Text += Environment.Version;
            
        }
        Exception e = new Exception();
    }}
form2含有四个按钮,用于抛出不同类型的异常,代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            throw new IndexOutOfRangeException();
        }        private void button2_Click(object sender, EventArgs e)
        {
            throw new InvalidOperationException();
        }        private void button3_Click(object sender, EventArgs e)
        {
            throw new InvalidCastException();
        }        private void button4_Click(object sender, EventArgs e)
        {
            throw new InvalidProgramException();
        }
    }
}
程序入口程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Form1 frm1 = new Form1();
                Application.Run(frm1);
            }
            catch (Exception e)
            {
                Form2 frm = new Form2(e);
                Application.Run(frm);
            }
    
        }
    }
}
可是为什么,当运行form1中的程序,然后点击按钮后,他出现的是系统的错误提示窗口,如下我想的是在我的form1中显示自己的东西。求大神求解

解决方案 »

  1.   

    补充一下,在main函数中各行加断点均挑不出去。
      

  2.   

    对不起,看错了。不过我试了一下你的程序,没有报错。
    我的报错了,你的form1出现了吗?我是想让它运行到fomr1但是它总是在form2点击一个按钮后,就报错了,然后不运行了。
      

  3.   

    想不出现系统的错误提示,就try一下。try
            {        }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString())
            }
    这样的话就是你自己的弹窗而不是系统的弹窗
      

  4.   

    搞定了,我果真是菜鸡啊,伤不起。用ctrl+f5就不可以,但是如果用f5调试的话就可以了,求大神指点,我上网查了一下,前者是不重新编译只显示结果,后者要重新编译,可为什么会这样。
      

  5.   

    你只抛出异常,没有捕获异常应该是这样try{throw new InvalidOperationException();}
    catch (Exception ex)
            {
                MessageBox.Show(ex.ToString())
            }
      

  6.   

            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += (sender, e) =>
                {
                    Form2 frm = new Form2(e);
                    frm.ShowDialog();
                }; 
            }