调试与发布(捕获异常) [英] Debug vs Release (catch exceptions)

查看:69
本文介绍了调试与发布(捕获异常)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我最近开始在Release Build中测试我的应用程序,执行速度的差异是惊人的(针对调试模式)。



我在main方法中有一个事件,用于保存执行期间发生的任何异常并将其保存在文件中。



这是代码



Hi all,

I recently started testing my application in Release Build, The diffrence in execution speed is amazing (against debug mode).

I have an event in the main method that save any exception that occur during execution and save it in a file.

Here is the code

[STAThread] 
        static void Main()
        {
//register the event 
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Updater());
            
        }
//if an exception occur in the app, it is saved in a file.
public static void CurrentDomain_UnhandledException(Object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                StreamWriter streamUnhadeled = new StreamWriter("erreurs non gérées.txt", true);

                streamUnhadeled.WriteLine("-----------------------------------"+ DateTime.Now.ToString() +"-----------------------------------------");                
                streamUnhadeled.WriteLine(ex.Message);
                streamUnhadeled.WriteLine(ex.StackTrace);
                streamUnhadeled.WriteLine(ex.InnerException);
                streamUnhadeled.WriteLine(ex.TargetSite);
                streamUnhadeled.WriteLine(ex.Source);
                      
                streamUnhadeled.Flush();

                streamUnhadeled.Close();
                streamUnhadeled.Dispose();


                MessageBox.Show("Oops ! Contactez nous "
                   + "avec les informations suivantes :\n\n" + ex.Message + ex.StackTrace,
                   "Erreur fatale", MessageBoxButtons.OK, MessageBoxIcon.Stop);

            }
            finally
            {
                Application.Exit();
            }

        }





在调试模式下,此事件(CurrentDomain_UnhandledException)工作正常,发布模式,它完全适用,当发生异常时应用程序关闭。



有人知道如何在Relase模式下获得异常吗?


谢谢



In Debug mode, this event(CurrentDomain_UnhandledException) works great, in Release mode, it dosent work at all, the application is closed when an exception occur.

Some one have any idea how to get exceptions in Relase mode?

Thanks

推荐答案

另一种方法是将主代码包装在try catch中。不确定为什么调试和发布之间存在差异。



Another approach is to wrap the main code in a try catch. Not sure why there is a difference between debug and release.

{
//register the event 
        try
        {
            AppDomain.CurrentDomain.UnhandledException +=
                new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
 
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Updater());
        }
        catch (Exception ex)
        {
            CaughtException(ex);
        }
      }
//if an exception occur in the app, it is saved in a file.
public static void CaughtException(Exception ex)
        {
            try
            {
                StreamWriter streamUnhadeled = new StreamWriter("erreurs non gérées.txt", true);
 
                streamUnhadeled.WriteLine("-----------------------------------"+ DateTime.Now.ToString() +"-----------------------------------------");                
                streamUnhadeled.WriteLine(ex.Message);
                streamUnhadeled.WriteLine(ex.StackTrace);
                streamUnhadeled.WriteLine(ex.InnerException);
                streamUnhadeled.WriteLine(ex.TargetSite);
                streamUnhadeled.WriteLine(ex.Source);
                      
                streamUnhadeled.Flush();
 
                streamUnhadeled.Close();
                streamUnhadeled.Dispose();
 

                MessageBox.Show("Oops ! Contactez nous "
                   + "avec les informations suivantes :\n\n" + ex.Message + ex.StackTrace,
                   "Erreur fatale", MessageBoxButtons.OK, MessageBoxIcon.Stop);
 
            }
            finally
            {
                Application.Exit();
            }
 
        }


哎呀,它与Release或debug无关,实际上发生了异常在流中,因为应用程序安装在Programefiles中,因此它是一个未经授权的访问异常。



感谢bowlturner和RyanDev的帮助。
Ooops, it have nothing to do with Release nor debug, actually there was an exception occuring in the stream because the application is installed in the Programefiles so its an unauthorized access exception.

Thanks to bowlturner and RyanDev for your assistance.

这篇关于调试与发布(捕获异常)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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