尝试/捕获 C# 中的所有异常

发布于 2024-09-09 21:33:40 字数 375 浏览 2 评论 0原文

我想用 C# 捕获 UI 应用程序中所有未处理的异常,以便我可以记录它们、通过邮件发送它们并重新启动应用程序。

我怎样才能简单地完成这件事?我尝试过:

try
{
 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new MainDlg());
}
catch (Exception e)
{
 Logger.Log(e);
 Logger.PostLog();
  System.Diagnostics.Process.Start("App.exe");
}

但它允许一些例外情况通过。

I would like to catch all unhandled exceptions in my UI app in C#, so that I can log them, send them by mail and restart the app.

How can I simply get this done? I tried:

try
{
 Application.EnableVisualStyles();
 Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new MainDlg());
}
catch (Exception e)
{
 Logger.Log(e);
 Logger.PostLog();
  System.Diagnostics.Process.Start("App.exe");
}

But it lets some exceptions through.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-09-16 21:33:41

您需要订阅AppDomain中的UnhandledException事件。请参阅 http://msdn.microsoft。 com/en-us/library/system.appdomain.unhandledexception(VS.71).aspx

You need to subscribe to the UnhandledException event in AppDomain. See http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception(VS.71).aspx

记忆里有你的影子 2024-09-16 21:33:40

您可以将此代码放入您的 Program.cs 中:

    static void Application_ThreadException(object sender,
        ThreadExceptionEventArgs e) //Generic error handler
    {
        MyApp.LogException(e.Exception); //Log exception

        BugReport frmBugReport = new BugReport();
        frmBugReport.Error = e.Exception;
        frmBugReport.ShowDialog();

        Environment.Exit(0);
    }

我在我的应用程序中显示错误报告对话框。您可以使用 Application.Restart() 更改Environment.Exit(0)(据我记得)。

You can put this code in your Program.cs:

    static void Application_ThreadException(object sender,
        ThreadExceptionEventArgs e) //Generic error handler
    {
        MyApp.LogException(e.Exception); //Log exception

        BugReport frmBugReport = new BugReport();
        frmBugReport.Error = e.Exception;
        frmBugReport.ShowDialog();

        Environment.Exit(0);
    }

I'm showing an error report dialog in my app. You can chage Environment.Exit(0) with Application.Restart() (As I remember).

回眸一笑 2024-09-16 21:33:40

Peter Bromberg 有一篇关于处理未处理异常的各种方法的好文章。
他描述了以下方法:

  1. 将 Application.Run() 放入 try-catch 块中
  2. 使用 Application.ThreadException 事件
  3. 使用 AppDomain.UnhandledException 事件
  4. 添加注册表项以弹出 JIT 调试器
  5. 在安装了调试符号的崩溃模式下使用 ADPLUS。

获取有关未处理异常的更好信息

关于我之前对该问题的评论:除非有紧迫的情况需要重新启动崩溃的应用程序 - 我不会自动重新启动应用程序,因为您可能会陷入循环。如果必须重新启动它,请在重新启动之前向用户提供重新启动应用程序的选择。这样,如果继续出错,用户可以退出。

Peter Bromberg has a good article on a variety of ways of dealing with unhandled exceptions.
He describes the following approaches:

  1. Putting Application.Run() in try-catch block
  2. Using Application.ThreadException event
  3. Using AppDomain.UnhandledException event
  4. Add registry entry to pop up JIT Debugger
  5. Use ADPLUS in crash mode with debug symbols installed.

Getting Better Information on Unhandled Exceptions

About my earlier comment on the question: unless there's a pressing need to restart the crashed application - I would not automatically restart the application, because of the loop you can get stuck in. If you must restart it, present the user with a choice to restart the application before restarting. That way, if it keeps going wrong, the user can bail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文