在C#中有没有办法从控件/表单中捕获所有异常?

编程入门 行业动态 更新时间:2024-10-28 10:27:26
本文介绍了在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#中有没有办法从控件/表单中捕获所有异常?

本文发布于:2023-11-03 14:24:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   没有办法   表单   控件   异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!