如何捕获winform程序全局异常?
1.在C#中我们如何处理异常?
上面的问题学过C#的问题大家可能都能回答处理,用try-catch-finally具体如下:
全局异常捕捉1[/caption]
这下能够实现我们的需求了,但是是有条件的,仅仅只有主线程一个线程的程序可以这么办,但是当程序的线程增加的时候,新增线程出错的时候我们还是无法捕捉到。具体代码如下:
try { //可能出错的语句 } catch (Exception) { //有异常的话进行处理 } finally { //有无异常都会从这里进行处理 }从上面的代码中我们可以知道,我们必须知道哪些语句可能有错误,才能进行预先用try-catch语句进行处理,现在代码量很大了,肯定有我们预期不到的情况,这个时候如果程序有了异常就可能引起程序的崩溃。那我们如何处理这样的异常呢? 理想当然的情况是,用一个try-catch把我们这个程序的代码都包活,可是这只是想想而已……那么具体的怎么实现呢?
2.下面提供一种方案供大家参考
好了,我们直接建立一个winform程序,然后上如下代码:using System; using System.Threading; using System.Windows.Forms; namespace TestAllException { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { //定义线程异常事件 ThreadExceptionHandler handler = new ThreadExceptionHandler(); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += handler.Application_ThreadException; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } internal class ThreadExceptionHandler { /// <summary> /// 实现错误异常事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Application_ThreadException(object sender,ThreadExceptionEventArgs e) { try { //如果用户点击Abort按钮则退出应用程序 DialogResult result = ShowThreadExcceptionDialog(e.Exception); if (result==DialogResult.Abort) { Application.Exit(); } } catch { try { MessageBox.Show("严重错误", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { Application.Exit(); } } } /// <summary> /// 输出错误异常 /// </summary> /// <param name="ex"></param> /// <returns></returns> private DialogResult ShowThreadExcceptionDialog(Exception ex) { string errorMessage = "错误信息:\n\n" + ex.Message + "\n\n" + ex.GetType() + "\n\nStack Trace\n\n" + ex.StackTrace; return MessageBox.Show(errorMessage,"Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); } } }
using System; using System.Threading; using System.Windows.Forms; namespace TestAllException { //form1下面对应的代码 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { throw new IndexOutOfRangeException("无效的异常操作"); } } }通过大家对上面的代码读,我就不直接上界面的图了,form1上只有一个button1的按钮。我们直接运行然后点击按钮,然后就会出现下面的情况: [caption id="attachment_1225" align="alignnone" width="600"]
![全局异常捕捉1](http://vsdot.net/wp-content/uploads/2013/07/20130720221059-1024x428.png)
using System; using System.Threading; using System.Windows.Forms; namespace TestAllException { 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) { try { Thread th = null; th = new Thread(new ThreadStart(ThreadStart1)); th.Start(); } catch (Exception ex) { throw ex; } } private void ThreadStart1() { throw new FormatException("多线程异常,格式异常"); } } }当上面的代码运行时点击button2时我们的异常无法处理,然后程序会崩溃,那么如何处理新增线程的异常呢?这个留到下篇文章开讲。