C# 控制台应用程序 - 防止默认异常对话框

发布于 2024-07-25 11:11:08 字数 719 浏览 3 评论 0原文

我有一个带有单个 AppDomain 的简单应用程序,该应用程序定期在服务器上启动。 有时应用程序中会出现未处理的异常,并且会弹出默认的中止/重试/忽略对话框。 我需要以某种方式阻止 edialog 显示并仅在 StrErr 上输出异常并关闭应用程序。 所以我用 try-catch 语句将 main 方法中的所有代码括起来,但它根本没有帮助 - 有时仍然会显示异常对话框。

Main() 代码如下所示:

try
{
    RunApplication();
}
catch (Exception exc)
{   
    Console.Error.WriteLine(exc.ToString());
    Console.Error.WriteLine(exc.StackTrace);
    if (exc.InnerException != null)
    {
       Console.Error.WriteLine(exc.InnerException.ToString());
       Console.Error.WriteLine(exc.InnerException.StackTrace);
    }
    Environment.Exit(666);
}

这个 try-catch 子句应该捕获所有未处理的异常,并且据我所知,异常对话框永远不应该弹出。 我错过了什么吗? 或者服务器上是否有任何设置(注册表等)来控制与异常对话框/应用程序错误代码相关的某些特殊行为?

I have simple application with single AppDomain which is periodicaly launched on a server. Sometimes unhandled exception occurs in the aplication and default abort/retry/ignore dialog pops up. I need to somehow prevent the edialog from showing and just output the exception on StrErr and close the application. So I enclosed all the code in main method with try-catch statement, but it didn't help at all - the exception dialog is still shown sometimes.

The Main() code looks like this:

try
{
    RunApplication();
}
catch (Exception exc)
{   
    Console.Error.WriteLine(exc.ToString());
    Console.Error.WriteLine(exc.StackTrace);
    if (exc.InnerException != null)
    {
       Console.Error.WriteLine(exc.InnerException.ToString());
       Console.Error.WriteLine(exc.InnerException.StackTrace);
    }
    Environment.Exit(666);
}

This try-catch clause shoud catch all unhandled exceptions and the exception dialog should never popup AFAIK. Am I missing something? Or is there any setting (registry etc) on the server which controls some special behaviour related to the exception dialog/application error code?

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

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

发布评论

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

评论(2

栩栩如生 2024-08-01 11:11:08

您可以在应用程序域中订阅未处理的异常事件。

    public static void Main()   
    {   
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

        //some code here....
    }   

    /// <summary>
    /// Occurs when you have an unhandled exception
    /// </summary>
    public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
    { 
        //here's how you get the exception  
        Exception exception = (Exception)e.ExceptionObject;  

        //bail out in a tidy way and perform your logging
    }

There's an unhandled exception event you can subscribe to in the application domain.

    public static void Main()   
    {   
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

        //some code here....
    }   

    /// <summary>
    /// Occurs when you have an unhandled exception
    /// </summary>
    public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
    { 
        //here's how you get the exception  
        Exception exception = (Exception)e.ExceptionObject;  

        //bail out in a tidy way and perform your logging
    }
傲性难收 2024-08-01 11:11:08

您是否考虑过您的 catch 子句可能引发异常的可能性?
您是否在主应用程序中生成线程?

Have you considered the possibility that your catch clause may be throwing exceptions?
Do you spawn threads in your main app?

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