建了两个窗体,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中显示自己的东西。求大神求解
解决方案 »
- 想做一个拖动图形的类,怎么实现呢
- DragObj.style 为空或者不是对象。
- 太菜,请教怎么写正则表达式抓网页中的 <a href="xxxx.pdf">文化娱乐</a>
- 怎么样使得 窗体显示在 屏幕中自定义位置?
- ---------高手指教啊!!!!!!!
- 怎样将一个combobox或textbox里的输入文本,从string型转为int型
- 怎样屏蔽win键
- 求一个已知图的拓扑排序算法
- 在一些方法和类上面有时候会出现一些方括弧, 是什么东东啊?
- Windows service 是否可以读取config文件里的配置?
- web 页面传值时,如何把字符转为对象名?
- 找到的程序集清单定义与程序集引用不匹配。
我的报错了,你的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();
};
}