简体   繁体   English

使用自定义例外表格

[英]Using A Custom Exception Form

I'm currently working on a C# WinForm application, and am trying to create a custom Form to use whenever an uncaught exception is thrown. 我目前正在使用C#WinForm应用程序,并且尝试创建一个自定义Form,以在引发未捕获的异常时使用。 The reason for this custom form, is to be able to log the details of every thrown exception in a log file, as well as provide the user with a sharp looking GUI with better, and easy to understand details of the error that occurred. 使用此自定义表单的原因是,能够将每个引发的异常的详细信息记录在日志文件中,并为用户提供外观清晰,外观更好,更易于理解的错误详细信息的GUI。

As it stands right now, i am registering for exception events: 就目前而言,我正在注册异常事件:

Application.ThreadException += new ThreadExceptionEventHandler(ExceptionHandler.OnThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionHandler.OnUnhandledException);

The ExceptionHandler class logs the error in a log file, and then displays my custom WinForm. ExceptionHandler类将错误记录在日志文件中,然后显示我的自定义WinForm。 My question is, is this the only way to display my custom form? 我的问题是,这是显示自定义表单的唯一方法吗? An issue i know of right now, is i am unable to determine whether the application can still continue, or if it will close when the form is closed. 我现在知道的一个问题是,我无法确定应用程序是否仍然可以继续运行,或者在关闭表单时是否将关闭。

Overall, my question is... Is there a better, or easier way to use my custom exception form? 总的来说,我的问题是...是否有更好或更简单的方式来使用我的自定义例外表格? Also, is there a way to know IF the application will be able to recover, once the Exception form is closed? 此外,一旦“异常”表关闭,是否有办法知道应用程序是否能够恢复?

It's not safe to continue running a program if you don't understand the reason for an exception. 如果您不了解发生异常的原因,则继续运行程序是不安全的。

This topic is discussed in more detail here: 这里将详细讨论该主题:

http://blogs.msdn.com/b/codeanalysis/archive/2006/06/14/631923.aspx http://blogs.msdn.com/b/codeanalysis/archive/2006/06/14/631923.aspx

You should start by exiting the application for any error you do not anticipate. 您应该先退出应用程序以应对未预料到的任何错误。 The form is a nice touch but beware it may not work, since you do not know what the problem is - so wrap that in a try catch. 表单是不错的选择,但是请注意,它可能不起作用,因为您不知道问题出在哪里,因此请将其包装在try catch中。 Then as errors come up do some analysis and if it is something you can recover from than catch that specific exception and recover. 然后在出现错误时进行一些分析,如果可以从中恢复,则可以捕获该特定异常并进行恢复。 Typically though if something bubbles all the way up to AppDomain.CurrentDomain.UnhandledException it is probably something that will leave the app in a bad place and you want to close. 通常,尽管如果某些事情一直持续到AppDomain.CurrentDomain.UnhandledException,它可能会使该应用程序陷入困境,并且您想要关闭。

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

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