locked
uncaught exception handlers RRS feed

  • Question

  • Hello everyone,

     


    I am new to how to catch uncaught exception. From some self-learning, I think we should use, event handler for AppDomain.CurrentDomain.UnhandledException other than Application.ThreadException if we are writing console or Windows service, right?

     

    Application.ThreadException is for Windows Form application, not console and Windows Service application?

     


    thanks in advance,
    George

     

    Saturday, April 26, 2008 2:53 PM

Answers

  • There's no need to overcomplicate things.  The Application class you're talking about is in the System.Windows.Forms assembly and namespace, so you wouldn't have it in a typical console application.

    Additionally, if you look at the code for the Application class, you will see that it is suited for an application that shows a form - it is coupled to WinForms.

    Additionally, if you try running the following sample you will see that the Application.ThreadException handler is not called.  So if the above didn't convince you yet that you're not supposed to be using the Application class in a console application, I hope you are now convinced.

        class Program
        {
            static void Main(string[] args)
            {
                Application.ThreadException += delegate
                {
                    Console.WriteLine("Application.ThreadException called.");
                };
                throw new InvalidOperationException();
            }
        }

    Sunday, April 27, 2008 5:35 AM
  • That's correct, a console app doesn't have an Application object.
    Saturday, April 26, 2008 7:13 PM

All replies

  • That's correct, a console app doesn't have an Application object.
    Saturday, April 26, 2008 7:13 PM
  •  Sasha Goldshtein wrote:
    That's correct, a console app doesn't have an Application object.

     

    Why is it so? I just opened one of my Console apps and typed:

     

    Application.Exit ( );

     

    I don't need to do it. The app exits because of the normal code run. It simply comes to the end of it but the statement was accepted by the IntelliSense and highlighted.

     

    Also when you are talking about an "Application Object" you have to define which one. MS invests many meanings in these two words. In different contexts these terms mean totally different things.

    Saturday, April 26, 2008 11:51 PM
  •  AlexBB wrote:

     Sasha Goldshtein wrote:
    That's correct, a console app doesn't have an Application object.

     

    And lo and behold, I opened the same application again and did, guess what?

     

    Application.ThreadException;  // it just poped up

     

    The IntelliSense is our best friend!

    Sunday, April 27, 2008 12:13 AM
  • There's no need to overcomplicate things.  The Application class you're talking about is in the System.Windows.Forms assembly and namespace, so you wouldn't have it in a typical console application.

    Additionally, if you look at the code for the Application class, you will see that it is suited for an application that shows a form - it is coupled to WinForms.

    Additionally, if you try running the following sample you will see that the Application.ThreadException handler is not called.  So if the above didn't convince you yet that you're not supposed to be using the Application class in a console application, I hope you are now convinced.

        class Program
        {
            static void Main(string[] args)
            {
                Application.ThreadException += delegate
                {
                    Console.WriteLine("Application.ThreadException called.");
                };
                throw new InvalidOperationException();
            }
        }

    Sunday, April 27, 2008 5:35 AM
  • Thanks Sasha,

     

     

    So we can only use "AppDomain.CurrentDomain.UnhandledException" for console and Windows service application?

     

     Sasha Goldshtein wrote:
    There's no need to overcomplicate things.  The Application class you're talking about is in the System.Windows.Forms assembly and namespace, so you wouldn't have it in a typical console application.

    Additionally, if you look at the code for the Application class, you will see that it is suited for an application that shows a form - it is coupled to WinForms.

    Additionally, if you try running the following sample you will see that the Application.ThreadException handler is not called.  So if the above didn't convince you yet that you're not supposed to be using the Application class in a console application, I hope you are now convinced.

        class Program
        {
            static void Main(string[] args)
            {
                Application.ThreadException += delegate
                {
                    Console.WriteLine("Application.ThreadException called.");
                };
                throw new InvalidOperationException();
            }
        }

     

     

    regards,

    George

    Sunday, April 27, 2008 2:22 PM
  • Yes.
    Sunday, April 27, 2008 2:33 PM
  • Thanks Sasha,

     

     

    Question answered.

     

     Sasha Goldshtein wrote:
    Yes.

     

     

    regards,

    George

    Sunday, April 27, 2008 2:42 PM
  •  Sasha Goldshtein wrote:
    .  So if the above didn't convince you yet that you're not supposed to be using the Application class in a console application, I hope you are now convinced.

     

    Yep, I actually realized when I was already in bed that I had System.Windows.Forms namespace referenced in that Console app. I think I needed one of the classes in there months ago. It was already too late, did not want to get up and I decided to be masochistic and expose myself to a public humiliationSmile

     

    You are correct.

    Sunday, April 27, 2008 3:05 PM
  • Thanks AlexBB,

     

     

    It is interesting that System.Windows.Forms namespace referenced in Concole application as you mentioned? But we can not use its class, like Application.Exit.

     

    If we can not use, why still referenced? :-)

     

     AlexBB wrote:

     Sasha Goldshtein wrote:
    .  So if the above didn't convince you yet that you're not supposed to be using the Application class in a console application, I hope you are now convinced.

     

    Yep, I actually realized when I was already in bed that I had System.Windows.Forms namespace referenced in that Console app. I think I needed one of the classes in there months ago. It was already too late, did not want to get up and I decided to be masochistic and expose myself to a public humiliation

     

    You are correct.

     

     

    regards,

    George

    Monday, April 28, 2008 5:25 AM
  •  George2 wrote:

    Thanks AlexBB,

     

     

    It is interesting that System.Windows.Forms namespace referenced in Concole application as you mentioned? But we can not use its class, like Application.Exit.

     

    If we can not use, why still referenced? :-)

     

     

     

     

    regards,

    George

     

    Answering: I needed the textBox class to communicate with another application (Windows.Forms namespace). The communcation line is named pipes but I wanted to have a detector to flag the event. I needed an event delegate. There is no visible textbox and it is just an instantiation but the TextChanged event is defined and every time the PipeClient gets a string from the other form it loads it into the TextBox as Text property. The next thing I have a chain of code making various decisions as to how to handle the message.

     

    I am sure there might be other ways and I can see some but that was perhaps the most conveninet for me to implement.

    Monday, April 28, 2008 2:41 PM
  • Thanks for clarification AlexBB,

     

     

    I think the answer for this thread should be, for console and Windows service application, we should use AppDomain.CurrentDomain.UnhandledException other than Application.ThreadException, agree? :-) 

     

     AlexBB wrote:

     George2 wrote:

    Thanks AlexBB,

     

     

    It is interesting that System.Windows.Forms namespace referenced in Concole application as you mentioned? But we can not use its class, like Application.Exit.

     

    If we can not use, why still referenced? :-)

     

     

     

    regards,

    George

     

    Answering: I needed the textBox class to communicate with another application (Windows.Forms namespace). The communcation line is named pipes but I wanted to have a detector to flag the event. I needed an event delegate. There is no visible textbox and it is just an instantiation but the TextChanged event is defined and every time the PipeClient gets a string from the other form it loads it into the TextBox as Text property. The next thing I have a chain of code making various decisions as to how to handle the message.

     

    I am sure there might be other ways and I can see some but that was perhaps the most conveninet for me to implement.

     

     

    regards,

    George

    Monday, April 28, 2008 2:46 PM
  • I think the most common use I've seen for a reference to WinForms from a console app is to display a message box :-)
    Monday, April 28, 2008 3:08 PM
  • I've got quite a few console apps that can display forms. For example, one has a /configure command-line option that will display a form for entering configuration data.

    Monday, April 28, 2008 3:32 PM
  • Thanks Sasha and Matthew,

     

     

    I think for unhanled exception in console or Windows service application without GUI, we can only use AppDomain.CurrentDomain.UnhandledException other than Application.ThreadException, right?

     

    (this is my question, but I agree with what you mentioned.) :-)

     

     Matthew Watson wrote:
    I've got quite a few console apps that can display forms. For example, one has a /configure command-line option that will display a form for entering configuration data.

     

     

    regards,

    George

    Tuesday, April 29, 2008 1:22 PM
  • George: That's correct.
    Tuesday, April 29, 2008 3:29 PM
  • Thanks Sasha,

     

     

    Question answered. I learned a lot from you in this thread. :-)

     

     Sasha Goldshtein wrote:
    George: That's correct.

     

     

    regards,

    George

    Saturday, May 3, 2008 8:33 AM