locked
How do you catch exceptions in threaded console application RRS feed

  • Question

  •  I was wondering how to catch thread exceptions in a console application. By looking online, I found out that if your application is a GUI application you can add an event handler to  Application.ThreadException. However, I don't know how to do this for a command line application.

    Friday, December 12, 2008 2:18 PM

Answers

  • Generally, you use Application.ThreadException if you developp a winform application.
    For global exception handling the framework provide the AppDomain.UnHandledException however you can't prevent the application to close on this event. It could be usefull for log purpose.

    I agree the main thread can't catch this exception, but if you add a try/catch inside the method called by the worker thread, it should prevent your application shutdown. However you have to add a try/catch in every method used by a thread.

    A solution is to use a BackgroundWorker.

    With the RunWorkerCompleted event you can check if an error has been raised :

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
          if (e.Error != null)
            Console.WriteLine("Exception: " + e.Error.ToString());
          else
            Console.WriteLine("End " + e.Result); 
    }
    • Marked as answer by Cebert Friday, December 12, 2008 5:31 PM
    Friday, December 12, 2008 4:11 PM
  • The 'Application ' class is a part of 'System.Windows.Forms' namespace


    Go to Solution explorer and Right-Click Refrences in your console application

    A box will pop-up and...... then add System.Windows.Form namespace.


    Now on the code window use it as Using System.Windows.Forms; (I hope you know how to to this ;) )


    Now you can use the Application.ThreadException.




    prs
    • Marked as answer by Cebert Friday, December 12, 2008 2:37 PM
    • Unmarked as answer by Cebert Friday, December 12, 2008 3:34 PM
    • Marked as answer by Cebert Friday, December 12, 2008 5:30 PM
    Friday, December 12, 2008 2:25 PM

All replies

  • The 'Application ' class is a part of 'System.Windows.Forms' namespace


    Go to Solution explorer and Right-Click Refrences in your console application

    A box will pop-up and...... then add System.Windows.Form namespace.


    Now on the code window use it as Using System.Windows.Forms; (I hope you know how to to this ;) )


    Now you can use the Application.ThreadException.




    prs
    • Marked as answer by Cebert Friday, December 12, 2008 2:37 PM
    • Unmarked as answer by Cebert Friday, December 12, 2008 3:34 PM
    • Marked as answer by Cebert Friday, December 12, 2008 5:30 PM
    Friday, December 12, 2008 2:25 PM
  • Thank you. This works. Is that generally a bad way to handle this problem? I have threads that are reading from IBM WebSphere queues. When they can't read from the queue for whatever reason they throw application exceptions.

    this is basically the main loop of these threads

    /// <summary>  
            /// Starts queue listener thread  
            /// </summary>  
            public void Start()  
            {  
                if (mStarted)  
                    throw new ApplicationException("QueueListener already started once");  
                if (mStopping)  
                    throw new ApplicationException("QueueLlistener has been stopped");  
                mStarted = true;  
                while (!mStopping)  
                {  
                    try 
                    {  
                        MQMessage msg = mGetClient.GetMessage();  
                        if (msg != null)  
                        {  
                            if (mRequestLogger != null)  
                                mRequestLogger(msg);  
                            if (mPutClient != null)  
                            {  
                                mPutClient.PutMessage(msg);  
                                Console.WriteLine("Queue Listener Placed Message On Queue" + mPutClient.QueueName);  
                            }  
                            else 
                            {  
                                Console.WriteLine("No put queue configured");  
                            }     
                        }  
                        else 
                        {  
                            Thread.Sleep(SleepTime);  
                        }  
                    }  
                    catch (ApplicationException e)  
                    {  
                        throw new ApplicationException("Listener encountered error " + e.Message);  
                    }  
                }  
                Console.WriteLine("Queue Listener for queue " + mGetClient.QueueName + " stopped");  
                mStopping = true;  
            } 
    Friday, December 12, 2008 2:36 PM
  • I should also note that Application.ThreadException doesn't really do what I thought it would
    Friday, December 12, 2008 2:49 PM
  • I don't know what "Microsoft" would have to say about this......wheteher this is bad or not. ;) he he he.

    From  my point of view correct way is to use try catch block.


    Regards.
    prs
    Friday, December 12, 2008 2:50 PM
  • A try catch block doesn't work. Threads have different stacks. The above code is running on its own thread. If a "worker thread" throws an exception, the main thread can not normally catch this exception and the application terminates with a nasty unhanded exception error.
     
    My main thread doesn't really need to know the exact details of the exception, but I would like the application to terminate in a more graceful manner.

    Friday, December 12, 2008 2:57 PM
  • Generally, you use Application.ThreadException if you developp a winform application.
    For global exception handling the framework provide the AppDomain.UnHandledException however you can't prevent the application to close on this event. It could be usefull for log purpose.

    I agree the main thread can't catch this exception, but if you add a try/catch inside the method called by the worker thread, it should prevent your application shutdown. However you have to add a try/catch in every method used by a thread.

    A solution is to use a BackgroundWorker.

    With the RunWorkerCompleted event you can check if an error has been raised :

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
          if (e.Error != null)
            Console.WriteLine("Exception: " + e.Error.ToString());
          else
            Console.WriteLine("End " + e.Result); 
    }
    • Marked as answer by Cebert Friday, December 12, 2008 5:31 PM
    Friday, December 12, 2008 4:11 PM