简体   繁体   English

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

[英]Is there a way in C# to catch all exceptions from a Control/Form?

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.对于从位于当前线程中的 Control 或 Form 内部抛出的异常,我想要一个“包罗万象”的东西,但不求助于使用整个线程的Application.ThreadException

Why?为什么? Because I'd like to keep the exception handling modular.因为我想保持异常处理模块化。

All the code that can cause the problems is being run within a Form ( DummyForm ) that is running within the current thread.所有可能导致问题的代码都在当前线程中运行的 Form ( DummyForm ) 中运行。 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 .我希望能够将所有这些都包装在其他一些异常( DummyFormException )中,该异常会将该 Form 识别为异常的原因,这样如果该异常出现在 Application.ThreadException 中,我就可以知道清理涉及关闭DummyForm

The alternatives, as I see them, are:在我看来,替代方案是:

# Wrap every single thrown exception in the DummyForm with a DummyFormException . # 在DummyForm中用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. # 在自己的线程中运行DummyForm ,并使用Application.ThreadException捕获它们,并识别代码中的线程。 Once I know where it comes from, I could simply close the DummyForm and end the thread.一旦我知道它来自哪里,我就可以简单地关闭DummyForm并结束线程。

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.但是,也许您需要的只是记录内容等的辅助方法。


Unhandled exceptions handlers (Application.ThreadException, AppDomain.CurrentDomain.UnhandledException) do exists, but that's not what they are for.未处理的异常处理程序(Application.ThreadException、AppDomain.CurrentDomain.UnhandledException)确实存在,但这不是它们的用途。

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.如果您知道某处可能发生异常,那么“某处”是编写异常处理代码的好地方。

Listen to Brann, otherwise you might end up on thedailywtf.com:-)听听 Brann,否则你可能会在dailywtf.com 上结束:-)

Leaving it to a global error handler will cause you all sorts of pain.将其留给全局错误处理程序会给您带来各种痛苦。 Make sure any exceptions in your application are handled at the source - that's the point at which you have the right information to decide how to handle it.确保您的应用程序中的任何异常都在源头处理 - 这就是您拥有正确信息来决定如何处理它的点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM