处理异常的最佳实践,它在catch块中抛出,在一个线程中。 (。净)

时间:2022-04-14 19:38:15

What's your opinion in handling exceptions within a thread's execution? More specifically, what if the exception is thrown inside catch block of an try-catch clause? And what happen to the thread if the exception is unhandled?

您对在线程执行中处理异常有何看法?更具体地说,如果在try-catch子句的catch块中抛出异常怎么办?如果异常未处理,线程会发生什么?

6 个解决方案

#1


What's your opinion in handling exceptions within a thread's execution?

您对在线程执行中处理异常有何看法?

You should handle exceptions whenever possible and whenever you expect exceptions. Clarification: I totally agree with John that you should not handle exceptions everywhere - only where you can do something about them. However, you should never let an exception go unhandled in a thread as this will cause serious problems. Have a root exception handler and let your thread die gracefully (after logging the problem etc...)

您应该尽可能地处理异常,并且只要您预期异常。澄清:我完全赞同约翰,你不应该处理各地的例外情况 - 只有你可以对它们做些什么。但是,您永远不应该在线程中处理异常,因为这会导致严重的问题。有一个root异常处理程序,让你的线程正常死亡(在记录问题之后......)

More specifically, what if the thread is thrown inside catch block of an try-catch clause?

更具体地说,如果线程被抛入try-catch子句的catch块中,该怎么办?

Did you mean: What if the exception is thrown within the catch block? Well, then it goes unhandled by the current try-catch block. It's best not to put too much processing in a catch block to avoid this situation as much as possible.

你的意思是:如果在catch块中抛出异常怎么办?好吧,然后它被当前的try-catch块无法处理。最好不要在catch块中放置太多处理以尽可能避免这种情况。

And what happen to the thread if the the the thread is unhandled?

如果线程未处理,线程会发生什么?

Did you mean: What happens to the thread if the exception is unhandled? It dies.

你的意思是:如果异常未处理,线程会发生什么?它死了。

And as Ben mentioned:

正如本提到的那样:

An uncaught exception in a thread triggers an UnhandledException in the thread's AppDomain. You can watch for these by adding an event handler:

线程中未捕获的异常会在线程的AppDomain中触发UnhandledException。您可以通过添加事件处理程序来监视这些:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

#2


I have to disagree with ren, or at least with what it sounds like he meant.

我不得不反对仁,或者至少听说他的意思。

Only handle exceptions if you can actually handle them. Only if you can do something about what went wrong, or add information. Don't handle them just because you can.

只有处理异常才能实际处理它们。只有你可以对出了什么问题做些什么,或者添加信息。不要因为你可以处理它们。

try {
    // ..
} catch (Exception ex) {
    Console.WriteLine(ex.Message);
}

The above is very bad. First, you don't display the entire exception, but only the Message. Second, you let things continue, and you don't know what state the process is in.

以上是非常糟糕的。首先,您不显示整个异常,而只显示消息。其次,你让事情继续下去,而你不知道这个过程处于什么状态。

#3


An uncaught exception in a thread triggers an UnhandledException in the thread's AppDomain. You can watch for these by adding an event handler:

线程中未捕获的异常会在线程的AppDomain中触发UnhandledException。您可以通过添加事件处理程序来监视这些:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

AppDomain.CurrentDomain.UnhandledException + = CurrentDomain_UnhandledException;

#4


Unhandled exceptions in a thread cause your process to die, and die with an unhelpful message in your event log.

线程中未处理的异常会导致进程死亡,并在事件日志中显示无用的消息。

Having top level exception handlers in every thread that log (and maybe re-throw) is a good practice, as is installing an appdomain exception handler as the other responses mention.

在每个记录(并且可能重新抛出)的线程中具有*异常处理程序是一种很好的做法,就像安装appdomain异常处理程序一样,正如其他响应所提到的那样。

#5


If you think you can handle an exception, you should catch it. All other exceptions that can not be dealt with should bubble up to the surface of the stack where if along the way no appropriate handler is found, will let the thread die.

You should not write any code in a catch block that could throw another exception, but if you need to you could nest another try catch block around it.

如果您认为可以处理异常,则应该抓住它。所有其他无法处理的异常应该冒泡到堆栈的表面,如果找不到合适的处理程序,将让线程死掉。你不应该在catch块中编写任何可能引发另一个异常的代码,但如果你需要,你可以在它周围嵌套另一个try catch块。

#6


The .net exception mechanisms fundamentally do not provide any good way to handle an exception occurs during the execution of a catch block, or during the execution of a "finally" block while another exception is pending. Proper handling would require that .net support a composite exception type which could be caught by "catch" blocks for any of its constituents, but which would be automatically rethrown up the call stack until all constituent parts had been dealt with. Unfortunately, .net doesn't provide any such composite exception types; while one could define and use custom types in such a way as to obtain such semantics, the code for using them would be rather ugly, and they wouldn't integrate well with exceptions other code might throw.

.net异常机制从根本上没有提供任何好的方法来处理在执行catch块期间发生的异常,或者在执行“finally”块期间,而另一个异常处于挂起状态。正确的处理将要求.net支持一个复合异常类型,它可以被任何组成部分的“catch”块捕获,但是在所有组成部分都被处理之前,它将自动重新向上调用栈。不幸的是,.net没有提供任何这样的复合异常类型;虽然可以以获得这种语义的方式定义和使用自定义类型,但使用它们的代码会相当丑陋,并且它们不能很好地与其他代码可能引发的异常集成。

Consequently, one is faced with a choice: stifle the new exception and let the old one propagate, let the new one propagate and lose the old one, or make a composite exception object which can only be handled by code that knows to look for it. None of those are particularly pleasant choices. Which one is best will depend upon an understanding of conditions what the different exception types represent, and what calling code will expect to do with them.

因此,一个人面临一个选择:扼杀新的异常并让旧的异常传播,让新的异常传播并丢失旧异常,或者创建一个复合异常对象,只能由知道寻找它的代码处理。这些都不是特别令人愉快的选择。哪一个是最好的将取决于对条件的理解,不同的异常类型代表什么,以及调用代码将期望用它们做什么。

#1


What's your opinion in handling exceptions within a thread's execution?

您对在线程执行中处理异常有何看法?

You should handle exceptions whenever possible and whenever you expect exceptions. Clarification: I totally agree with John that you should not handle exceptions everywhere - only where you can do something about them. However, you should never let an exception go unhandled in a thread as this will cause serious problems. Have a root exception handler and let your thread die gracefully (after logging the problem etc...)

您应该尽可能地处理异常,并且只要您预期异常。澄清:我完全赞同约翰,你不应该处理各地的例外情况 - 只有你可以对它们做些什么。但是,您永远不应该在线程中处理异常,因为这会导致严重的问题。有一个root异常处理程序,让你的线程正常死亡(在记录问题之后......)

More specifically, what if the thread is thrown inside catch block of an try-catch clause?

更具体地说,如果线程被抛入try-catch子句的catch块中,该怎么办?

Did you mean: What if the exception is thrown within the catch block? Well, then it goes unhandled by the current try-catch block. It's best not to put too much processing in a catch block to avoid this situation as much as possible.

你的意思是:如果在catch块中抛出异常怎么办?好吧,然后它被当前的try-catch块无法处理。最好不要在catch块中放置太多处理以尽可能避免这种情况。

And what happen to the thread if the the the thread is unhandled?

如果线程未处理,线程会发生什么?

Did you mean: What happens to the thread if the exception is unhandled? It dies.

你的意思是:如果异常未处理,线程会发生什么?它死了。

And as Ben mentioned:

正如本提到的那样:

An uncaught exception in a thread triggers an UnhandledException in the thread's AppDomain. You can watch for these by adding an event handler:

线程中未捕获的异常会在线程的AppDomain中触发UnhandledException。您可以通过添加事件处理程序来监视这些:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

#2


I have to disagree with ren, or at least with what it sounds like he meant.

我不得不反对仁,或者至少听说他的意思。

Only handle exceptions if you can actually handle them. Only if you can do something about what went wrong, or add information. Don't handle them just because you can.

只有处理异常才能实际处理它们。只有你可以对出了什么问题做些什么,或者添加信息。不要因为你可以处理它们。

try {
    // ..
} catch (Exception ex) {
    Console.WriteLine(ex.Message);
}

The above is very bad. First, you don't display the entire exception, but only the Message. Second, you let things continue, and you don't know what state the process is in.

以上是非常糟糕的。首先,您不显示整个异常,而只显示消息。其次,你让事情继续下去,而你不知道这个过程处于什么状态。

#3


An uncaught exception in a thread triggers an UnhandledException in the thread's AppDomain. You can watch for these by adding an event handler:

线程中未捕获的异常会在线程的AppDomain中触发UnhandledException。您可以通过添加事件处理程序来监视这些:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

AppDomain.CurrentDomain.UnhandledException + = CurrentDomain_UnhandledException;

#4


Unhandled exceptions in a thread cause your process to die, and die with an unhelpful message in your event log.

线程中未处理的异常会导致进程死亡,并在事件日志中显示无用的消息。

Having top level exception handlers in every thread that log (and maybe re-throw) is a good practice, as is installing an appdomain exception handler as the other responses mention.

在每个记录(并且可能重新抛出)的线程中具有*异常处理程序是一种很好的做法,就像安装appdomain异常处理程序一样,正如其他响应所提到的那样。

#5


If you think you can handle an exception, you should catch it. All other exceptions that can not be dealt with should bubble up to the surface of the stack where if along the way no appropriate handler is found, will let the thread die.

You should not write any code in a catch block that could throw another exception, but if you need to you could nest another try catch block around it.

如果您认为可以处理异常,则应该抓住它。所有其他无法处理的异常应该冒泡到堆栈的表面,如果找不到合适的处理程序,将让线程死掉。你不应该在catch块中编写任何可能引发另一个异常的代码,但如果你需要,你可以在它周围嵌套另一个try catch块。

#6


The .net exception mechanisms fundamentally do not provide any good way to handle an exception occurs during the execution of a catch block, or during the execution of a "finally" block while another exception is pending. Proper handling would require that .net support a composite exception type which could be caught by "catch" blocks for any of its constituents, but which would be automatically rethrown up the call stack until all constituent parts had been dealt with. Unfortunately, .net doesn't provide any such composite exception types; while one could define and use custom types in such a way as to obtain such semantics, the code for using them would be rather ugly, and they wouldn't integrate well with exceptions other code might throw.

.net异常机制从根本上没有提供任何好的方法来处理在执行catch块期间发生的异常,或者在执行“finally”块期间,而另一个异常处于挂起状态。正确的处理将要求.net支持一个复合异常类型,它可以被任何组成部分的“catch”块捕获,但是在所有组成部分都被处理之前,它将自动重新向上调用栈。不幸的是,.net没有提供任何这样的复合异常类型;虽然可以以获得这种语义的方式定义和使用自定义类型,但使用它们的代码会相当丑陋,并且它们不能很好地与其他代码可能引发的异常集成。

Consequently, one is faced with a choice: stifle the new exception and let the old one propagate, let the new one propagate and lose the old one, or make a composite exception object which can only be handled by code that knows to look for it. None of those are particularly pleasant choices. Which one is best will depend upon an understanding of conditions what the different exception types represent, and what calling code will expect to do with them.

因此,一个人面临一个选择:扼杀新的异常并让旧的异常传播,让新的异常传播并丢失旧异常,或者创建一个复合异常对象,只能由知道寻找它的代码处理。这些都不是特别令人愉快的选择。哪一个是最好的将取决于对条件的理解,不同的异常类型代表什么,以及调用代码将期望用它们做什么。