catch块在另一个线程中没有捕获异常

Sle*_*ght 2 c# multithreading try-catch

method A()
{
  try
  {
    Thread t = new Thread(new ThreadStart(B));
    t.Start();
  }
  catch(exception e)
  {
    //show message of exception
  }      

}

method B()
{
 // getDBQuery
}
Run Code Online (Sandbox Code Playgroud)

B中的例外但没有捕获.它在.net中合法吗?

Hen*_*man 7

正确,Thread中的异常不会转发给调用者,Thread应该自己处理.

最一般的答案是你不应该在这里使用(裸)线程.它效率不高,不方便.

当您使用任务时,在您Wait()或者cal时会存储和引发异常Result.