捕获在单独的线程未处理的异常 [英] Catching unhandled exception on separate threads
问题描述
我用下面的事件来捕获未处理的主UI线程例外。
I am using the following event to catch unhandled exceptions in the main UI thread.
Application.ThreadException
不幸的是,它不捕获在单独的线程那些未处理的错误。我知道
Unfortunately, it does not catch those unhandled errors in seperate threads. I am aware of
AppDomain.CurrentDomain.UnhandledException
不过,这似乎触发后,这里因为前者不会关闭应用程序。
However, this seems to shut down the application upon triggering, where as the former does not.
有没有办法来处理在单独的线程未处理的异常,如果没有申请截止?
Is there a way to deal with unhandled exceptions on separate threads, without the application closing?
推荐答案
@Ani已经回答了你的问题。虽然我不同意线程未处理的异常应终止应用程序。使用线程通常意味着你有某种服务器应用程序。将其击落,可能会导致大批愤怒的用户。
@Ani have already answered your question. Although I don't agree that unhandled exceptions in threads should terminate applications. Using threads usually means that you have some kind of server application. Bringing it down could result in a lot of angry users.
我已经写适当的异常处理一小部分:<一href=\"http://blog.gauffin.org/2010/11/do-not-catch-that-exception/\">http://blog.gauffin.org/2010/11/do-not-catch-that-exception/
I've written a small piece about proper exception handling: http://blog.gauffin.org/2010/11/do-not-catch-that-exception/
您应该总是赶线程异常。我通常使用以下模式:
You should always catch exceptions for threads. I usually use the following pattern:
void ThreadMethod(object state)
{
try
{
ActualWorkerMethod();
}
catch (Exception err)
{
_logger.Error("Unhandled exception in thread.", err);
}
}
void ActualWorkerMethod()
{
// do something clever
}
这是一大堆更容易找到线程的方法,它不是通过逻辑移动到一个单独的方法处理异常妥善和公正保持try / catch块中的线程的方法。
It's a whole lot easier to find thread methods that doesn't handle exceptions properly by moving the logic into a seperate method and just keep the try/catch block in the thread method.
这篇关于捕获在单独的线程未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!