我有与在服务器上发起periodicaly单个应用程序域的简单应用。有时在aplication和默认中止/重试/忽略对话框弹出发生未处理的异常。我需要以某种方式阻止显示,只是输出上StrErr异常的edialog和关闭应用程序。所以,我写在用try-catch语句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子句768,16捕捉所有未处理的异常和异常对话不应该弹出AFAIK。我缺少的东西吗?或者有没有在服务器上控制相关的例外对话框/应用程序错误代码的一些特殊行为的任何设置(注册表等)?

有帮助吗?

解决方案

有未处理的异常事件时,可以在应用程序域订阅。

    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
    }

其他提示

你有没有考虑你的catch子句可能会抛出异常的可能性? 你生成线程在你的主应用程序?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top