表单中捕获所有异常

表单中捕获所有异常

本文介绍了在C#中有没有办法从控件/表单中捕获所有异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对位于当前线程内的控件或表单内部的异常抛出异常,但不要使用 Application.ThreadException 整个线程。

I'd like to have something like a "catch-all" for exceptions thrown from a inside a Control or a Form that reside within the current thread, but without resorting to using the Application.ThreadException of the entire thread.

为什么?因为我想保持异常处理模块化。

Why? Because I'd like to keep the exception handling modular.

所有可能导致问题的代码都是在一个Form( DummyForm )在当前线程中运行。我希望能够将所有这些都包含在某些其他例外( DummyFormException )中,这将会将该表单标识为异常的原因,以便如果该异常它到Application.ThreadException,我可以知道清理涉及关闭 DummyForm

All the code that can cause the problems is being run within a Form (DummyForm) that is running within the current thread. I'd like to be able to wrap all of them in some other exception (DummyFormException) that would identify that Form as the cause of the exception, so that if that exception makes it to the Application.ThreadException, I can know that the cleanup involves closing DummyForm.

替代方案,因为我看到它们是:

The alternatives, as I see them, are:

#将 DummyForm 中的每一个抛出的异常包含一个 DummyFormException 。我不喜欢这么多,因为它需要未来的程序员记住包装每个抛出的异常。
#在自己的线程中运行 DummyForm ,并使用 Application.ThreadException 来捕获它们,并识别该代码内的线程。一旦我知道它来自哪里,我可以简单地关闭 DummyForm 并结束线程。

# Wrap every single thrown exception in the DummyForm with a DummyFormException. I don't like this so much, since it requires future programmers to remember to wrap each thrown exception, too. # Run the DummyForm in its own thread, and use Application.ThreadException to catch them, and identify the thread inside that code. Once I know where it comes from, I could simply close the DummyForm and end the thread.

有没有这样做不用去多线程呢?我不喜欢这样做,似乎只是为了错误处理浪费。或者我会这样做都错了?

Is there any way to do this without going multi-threaded? I'd hate to do it, it seems a waste just for error-handling. Or am I going about this all wrong?

推荐答案

我不知道把所有的错误处理代码在一个地方。

I'm not sure it's a good idea to put all the error handling code in a single place.

错误处理应该更好地在其处理的操作旁边。

Error handling should better take place next to the operation it handles.

整个错误处理点是更改程序遇到错误的程序行为。这意味着您必须在该位置编写特定的代码,并且您不能将所有内容放在一个似乎想要的位置。

The whole point of error handling is to change the program behaviour where the program encounters an error. This means you have to write specific code at that location, and that you can't put everything in only one place as you seem to want to.

但是,您所需要的只是帮助方法来记录内容等。

However, maybe what you need is only an helper method to log stuff, etc.

未处理的异常处理程序(Application.ThreadException,AppDomain.CurrentDomain.UnhandledException)存在,但这不是它们的内容。

Unhandled exceptions handlers (Application.ThreadException, AppDomain.CurrentDomain.UnhandledException) do exists, but that's not what they are for.

使用这些来处理意外的异常,即发生错误的事情,你没有想到,你想要很好地处理这种情况(例如,你可能想要记录错误或显示一个很好的错误框给用户,或者轻轻地关闭程序)

You should use those to handle unexpected exceptions, ie, something wrong occurred that you didn't think of, and you want to handle this case nicely (For example, you may want to log the error, or display a nice error box to the user, or gently close the program)

如果您知道某个异常可能发生在某个地方,那么某处是编写异常处理代码的好地方。 em>

If you know an Exception may occur somewhere, that "somewhere" is the good place to write your exception handling code.

这篇关于在C#中有没有办法从控件/表单中捕获所有异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:53