建了两个窗体,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中显示自己的东西。求大神求解
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中显示自己的东西。求大神求解
我的报错了,你的form1出现了吗?我是想让它运行到fomr1但是它总是在form2点击一个按钮后,就报错了,然后不运行了。
{ }
catch (Exception ex)
{
MessageBox.Show(ex.ToString())
}
这样的话就是你自己的弹窗而不是系统的弹窗
catch (Exception ex)
{
MessageBox.Show(ex.ToString())
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (sender, e) =>
{
Form2 frm = new Form2(e);
frm.ShowDialog();
};
}