Application.ThreadException不处理.NET4中的异常 [英] Application.ThreadException does not handle exceptions in .NET4

查看:183
本文介绍了Application.ThreadException不处理.NET4中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看以下代码



Look the following code

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
        static void Main()
        {

            #region Capturnig unhandled exceptions

            //Generate detailed information about an exception
            var GetExceptionDetails = new Func<Exception,string>(ex=>
            {
                string result = "";
                result += "Source of the error: " + ex.Source;
                result += "\nStack trace: " + ex.StackTrace;
                result += "\nError message: " + ex.Message;
                return result;
            });

            // Set the unhandled exception mode to force all Windows Forms errors to go through
            // our handler.
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

            // Add the event handler for handling UI thread exceptions to the event.
            Application.ThreadException += delegate(object sender, System.Threading.ThreadExceptionEventArgs e)
            {
                Library.Data.Logger.AddState("An error has been occured",GetExceptionDetails(e.Exception) );
            };


            // Add the event handler for handling non-UI thread exceptions to the event.
            AppDomain.CurrentDomain.UnhandledException += delegate(object sender, UnhandledExceptionEventArgs e)
            {
                Library.Data.Logger.AddState("An error has been occured",GetExceptionDetails((Exception)e.ExceptionObject));
            };


            #endregion



            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Forms.SplashForm());
        }



    }





但它没有捕获以下异常





But it didn't catch the following exception

public class SplashFor:Form
{
public SplashForm()
{
int i = 6/int.Parse("0");
}
}





有什么问题?



What is the problem?

推荐答案

在但它没有捕获以下异常中,以下不是例外,期间。这个问题毫无意义。也许你确实在某个地方有一些例外,但你的问题不是例外,但缺乏理解而且未能提供全面的问题报告。



好​​吧, Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)是一个完美的想法,但1)在设置此模式和处理程序之前可能会发生一些异常,2)它只处理UI线程中的异常;对于其他线程,你应该设计自己的处理所有异常的机制(不出所料,基于try-catch :-))。



此外,它是可能你只是认为你应该有一个例外,但它实际上并没有被抛出。也就是说,它被抓住了,但是......如果它被抛出了。但是, int i = 6 / int.Parse(0); 抛出整数除零异常;它没有必要解析。 :-)



-SA
In "But it didn't catch the following exception" the "following" is not exception, period. The question makes no sense at all. Maybe you really have some exception somewhere, but your problem is not exception, but lack of understanding and the failure to present a comprehensive issue report.

Well, Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException) is a perfect idea, but 1) some exceptions may happen before you setup this mode and the handler, 2) it handles only the exception in your UI thread; for other threads, you should devise your own mechanism of dealing with all its exception (not surprisingly, based on try-catch :-)).

Moreover, it is possible that you just think that you should have an exception, but it is not actually thrown. That is, it is caught, but… if it was thrown. But yes, int i = 6/int.Parse("0"); throws integer division by zero exception; it was no need to "parse". :-)

—SA


这篇关于Application.ThreadException不处理.NET4中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆