程序结构如下
public A
{
  public Start()  
  {
     Thread.Start(B);
  }
  B()
  {
     throw new Exception();
  }
}
public Test
{
   public static void Main()
   {
      A a=new A();
      a.Start();
   }
}想在main中处理B抛出来的异常,该怎么做?
请大家不吝赐教。

解决方案 »

  1.   

    public Test 

      public static void Main() 
      {
        try
        { 
          A a=new A(); 
          a.Start();
        }
        catch
        {
           // 处理异常
         } 
      } 
      

  2.   

    Winform程序,大概可以使用Application.ThreadException事件来处理。
    Console程序,大概可以用AppDomain.UnhandledException 事件来处理。
      

  3.   

    这是MSDN中使用AppDomain.UnhandledException 事件的例子:using System;
    using System.Security.Permissions;public class Test {   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
       public static void Example()
       {
          AppDomain currentDomain = AppDomain.CurrentDomain;
          currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);      try {
             throw new Exception("1");
          } catch (Exception e) {
             Console.WriteLine("Catch clause caught : " + e.Message);
          }      throw new Exception("2");      // Output:
          //   Catch clause caught : 1
          //   MyHandler caught : 2
       }   static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
          Exception e = (Exception) args.ExceptionObject;
          Console.WriteLine("MyHandler caught : " + e.Message);
       }   public static void Main() {
          Example();
       }
    }
      

  4.   

    线程不支持异常抛出
    线程中的方法可以
    异步执行的线程,winfrom中可以用backgroundworker来搞
      

  5.   


    Thread th = new Thread(new ThreadStart(DoSplash));            th.Start();            Thread.Sleep(5000);            try
                {
                    th.Abort();//引发异常
                }
                catch (ThreadAbortException)
                {
                    //处理异常
                }            Thread.Sleep(1000);
      

  6.   

    自己弄了个笨办法public A 

      EventWaitHandle whException;
      public Start()  
      { 
        whException=new AutoResetEvent(false);
        Thread.Start(B);
        whException.WaitOne();
        throw new Exception();  
      } 
      B() 
      { 
        whException.Set(); 
      } 

    public Test 

      public static void Main() 
      { 
        try
        {  
          A a=new A(); 
          a.Start(); 
        }
        catch
        {
        }
      } 
      

  7.   

    这么做有个问题,start停不下来