当.NET线程抛出一个异常,会发生什么? [英] What happens when a .NET thread throws an exception?

查看:193
本文介绍了当.NET线程抛出一个异常,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个接​​口IPoller对此我们有不同的实现。我们有一个过程,将采取的IPoller和在单独的线程启动它。我试图想出提供异常处理任何IPollers不自己做了一个通用的方法。

We have an interface IPoller for which we have various implementations. We have a process that will take an IPoller and start it in a separate thread. I'm trying to come up with a generic way of providing exception handling for any IPollers which don't do it themselves.

我最初的想法是创建IPoller的实现,将接受IPoller,只是提供一些日志记录功能。我虽然碰到的问题是如何将我提供这个错误处理?如果我有IPoller.Start(),这是该线程的目标是,将发生异常?或者是有什么线程本身上我可以挂接到?

My original thinking was to create an implementation of IPoller that would accept an IPoller and just provide some logging functionality. The question I ran into though is how would I provide this error handling? If I have IPoller.Start() which is the target for the Thread is that where the exception will occur? Or is there something on the thread itself I can hook into?

推荐答案

是这样的:

Thread thread = new Thread(delegate() {
    try
    {
        MyIPoller.Start();
    }
    catch(ThreadAbortException)
    {
    }
    catch(Exception ex)
    {
        //handle
    }
    finally
    {
    }
});

这将确保异常不使其向螺纹的顶部。

This will ensure the exception doesn't make it to the top of the thread.

这篇关于当.NET线程抛出一个异常,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆