![c#线程池中的异常 c#线程池中的异常](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700)
static void Main(string[] args)
{
//写日志
//使用线程池
for (int i = ; i < ; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveMessage), i);
} Console.ReadKey();
} private static void ReceiveMessage(object index)
{
Thread.Sleep();
int b = ;
try
{
if (index.ToString() == "")
{
Console.WriteLine( / b);
}
else
{
Console.WriteLine(index.ToString());
}
}
catch (Exception)
{
Console.WriteLine(index+":出错");
} }
以上是测试代码,结论如下:
1.线程池中的异常不做处理:会导致整个程序的闪退
2.针对异常代码添加异常处理机制:不影响其他线程的执行