Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Look the following code

C#
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

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


What is the problem?
Posted
Comments
Herman<T>.Instance 13-Aug-14 10:21am    
did you debug?
Sergey Alexandrovich Kryukov 13-Aug-14 11:58am    
Where is the exception? You can always have a stack trace.
—SA

1 solution

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
 
Share this answer
 
Comments
Mohammed Asaad 2-Sep-14 0:55am    
Thanks Sergey :)
Sergey Alexandrovich Kryukov 2-Sep-14 2:26am    
You are very welcome.
Good luck, call again.
—SA

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