主线程捕捉子线程的异常

    首先需要了解异常的实现机制:异常的实现机制是严重依赖与线程的栈的。每个线程都有一个栈,线程启动后会在栈上安装一些异常处理帧,并形成一个链表的结构,在异常发生时通过该链表可以进行栈回滚,如果你自己没有安装的话,可能会直接跳到链表尾部,那可能是CRT提供的一个默认处理帧,弹出一个对话框提示某某地址内存错误等,然后确认调试,取消关闭。所以说,线程之间是不可能发生异常处理的交换关系的。

   下面例子,用委托来实现异常捕获

  Demo:

  1 namespace WindowsFormsApplication4
  2 {
  3     /// <summary>
  4     /// 自定义异常
  5     /// </summary>
  6     public class WorkFlowException : Exception
  7     {
  8         public WorkFlowException(int code, string type = "", string message = "")
  9         {
 10             ErrorCode = code;
 11             ErrorType = type;
 12             ErrorMessage = message;
 13         }
 14         int errorCode;
 15         /// <summary>
 16         /// 错误代码
 17         /// </summary>
 18         public int ErrorCode
 19         {
 20             get { return errorCode; }
 21             set { errorCode = value; }
 22         }
 23         string errorType;
 24         /// <summary>
 25         /// 错误类型
 26         /// </summary>
 27         public string ErrorType
 28         {
 29             get { return errorType; }
 30             set { errorType = value; }
 31         }
 32         string errorMessage;
 33         /// <summary>
 34         /// 错误信息
 35         /// </summary>
 36         public string ErrorMessage
 37         {
 38             get { return errorMessage; }
 39             set { errorMessage = value; }
 40         }
 41     }
 42 
 43     public partial class Form1 : Form
 44     {
 45         public Form1()
 46         {
 47             InitializeComponent();
 48         }
 49 
 50         Thread tWork = null;
 51         private void Form1_Load(object sender, EventArgs e)
 52         {
 53             Parameters.ExceptionEventer = new Parameters.ExceptionHandler(ProcessException);
 54             tWork = new Thread(() =>
 55             {
 56                 try
 57                 {
 58                     ThreadTest tt = new ThreadTest();
 59                     tt.Compute();
 60                     Thread.Sleep(1000);
 61                     MessageBox.Show("线程结束");
 62                 }
 63                 catch (WorkFlowException ex)
 64                 {
 65                     MessageBox.Show("1");
 66                 }
 67             });
 68             tWork.Start();
 69         }
 70 
 71         /// <summary>
 72         /// 异常处理
 73         /// </summary>
 74         /// <param name="ex"></param>
 75         private void ProcessException(Exception ex)
 76         {
 77             /*主线程终止*/
 78             tWork.Abort();
 79             MessageBox.Show(ex.Message);
 80         }
 81     }
 82 
 83     public static class Parameters
 84     {
 85         public delegate void ExceptionHandler(Exception ex);
 86         public static ExceptionHandler ExceptionEventer;
 87     }
 88 
 89     public class ThreadTest
 90     {
 91         public void Compute()
 92         {
 93 
 94             Thread t = new Thread(() =>
 95             {
 96                 try
 97                 {
 98                     (new WorkTest()).Compute();
 99                 }
100                 catch (Exception ex)
101                 {
102                     Parameters.ExceptionEventer(ex);
103                 }
104             });
105             t.Start();
106         }
107     }
108 
109     public class WorkTest
110     {
111         public int a = 0;
112         public int b = 0;
113         public void Compute()
114         {
115             try
116             {
117                 int c = a / b;
118             }
119             catch (Exception ex)
120             {
121                 throw ex;
122             }
123         }
124     }
125 }
View Code

 

 

posted on 2014-10-17 10:35  象山  阅读(1043)  评论(0)    收藏  举报

导航