Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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

C#
[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();
            }

        }


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
Posted
Comments
ZurdoDev 9-Jan-14 10:22am    
The description here might help: http://social.msdn.microsoft.com/Forums/en-US/22307a23-8df8-4816-98f1-1b1b9fef89de/appdomaincurrentdomainunhandledexception-new?forum=csharplanguage

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();
            }
 
        }
 
Share this answer
 
Comments
Ziee-M 9-Jan-14 10:50am    
actually the deployed version in release mode don't detect the error,
in deubg mode, deployed version work fine!

i will try this approch too hope it works
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.
 
Share this answer
 
Comments
Yvan Rodrigues 9-Jan-14 19:55pm    
Yes, new StreamWriter() will throw an exception under many possible conditions, and you will never know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900