有没有在C#办法赶上从控制/表格所有的异常? [英] Is there a way in C# to catch all exceptions from a Control/Form?

查看:141
本文介绍了有没有在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.

所有的,可能会导致问题的代码被一个窗体中运行( 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.

不过,也许你所需要的仅仅是记录的东西的helper方法,等等。

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)

如果您知道可能什么地方发生异常,的说,地方是好地方写你的异常处理代码。

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

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

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