Click here to Skip to main content
15,914,608 members
Home / Discussions / C#
   

C#

 
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h28-Apr-08 19:49
N a v a n e e t h28-Apr-08 19:49 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 2:25
George_George29-Apr-08 2:25 
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h29-Apr-08 3:45
N a v a n e e t h29-Apr-08 3:45 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 4:21
George_George29-Apr-08 4:21 
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h29-Apr-08 19:44
N a v a n e e t h29-Apr-08 19:44 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 20:23
George_George29-Apr-08 20:23 
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h30-Apr-08 17:01
N a v a n e e t h30-Apr-08 17:01 
GeneralRe: uncaught exception handlers Pin
George_George1-May-08 2:42
George_George1-May-08 2:42 
Thanks N a v a n e e t h,


I agree with all of your points, except this one,

N a v a n e e t h wrote:
Note we handled the exception in the same thread where asynchronous method is executing, not in the main thread.


My point is we should handle it in main thread. Here is my code to prove. Any comments?

(sorry the code is a little messy, I used it to test for multi-purpose in this discussion.)

using System;
using System.Threading;

namespace DelegateThread
{
    class Test
    {
        private static void Method2 (object input)
        {
            Console.WriteLine("Method1 is throwing exception");
            throw new ApplicationException("**** oops ****");
        }

        private static void Method1()
        {
            Console.WriteLine("Method1 is throwing exception");
            throw new ApplicationException("**** oops ****");
        }

        private static void Handler1 (object sender, EventArgs e)
        {
            Console.WriteLine ("I am here");
        }

        private static void Handler3(object sender, EventArgs e)
        {
            Console.WriteLine("I am here");
        }

        delegate void Method1Delegate();

        static void Main(string[] args)
        {
            Console.WriteLine("We first use a thread");
/*
            AppDomain.CurrentDomain.ProcessExit += new EventHandler (Test.Handler1);
            AppDomain.CurrentDomain.UnhandledException +=new UnhandledExceptionEventHandler(Test.Handler3);
 */
      /*      
            Thread aThread
                = new Thread(new ThreadStart(Method1));
            aThread.Start();
        */

            // ThreadPool.QueueUserWorkItem(new WaitCallback(Test.Method2));

            
            Console.WriteLine("We will use a Delegate now");
            Method1Delegate dlg = new Method1Delegate(Method1);
            IAsyncResult handle = dlg.BeginInvoke(null, null);
            
            Thread.Sleep(1000);

            Console.WriteLine("Was the exception reported so far?");

            try
            {
                Console.WriteLine("Let's call EndInvoke");
                dlg.EndInvoke(handle);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
                


            Thread.Sleep(5000);

            Console.WriteLine("Survived exception");

            return;

        } // main
    }
}



regards,
George
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h1-May-08 6:05
N a v a n e e t h1-May-08 6:05 
GeneralRe: uncaught exception handlers Pin
George_George1-May-08 21:38
George_George1-May-08 21:38 
GeneralRe: uncaught exception handlers Pin
#realJSOP27-Apr-08 3:08
professional#realJSOP27-Apr-08 3:08 
GeneralRe: uncaught exception handlers Pin
George_George27-Apr-08 3:32
George_George27-Apr-08 3:32 
GeneralRe: uncaught exception handlers Pin
Spacix One28-Apr-08 7:02
Spacix One28-Apr-08 7:02 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 2:20
George_George29-Apr-08 2:20 
GeneralRe: uncaught exception handlers Pin
N a v a n e e t h29-Apr-08 3:48
N a v a n e e t h29-Apr-08 3:48 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 4:24
George_George29-Apr-08 4:24 
GeneralRe: uncaught exception handlers Pin
Spacix One29-Apr-08 4:21
Spacix One29-Apr-08 4:21 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 4:26
George_George29-Apr-08 4:26 
GeneralRe: uncaught exception handlers Pin
Spacix One29-Apr-08 5:31
Spacix One29-Apr-08 5:31 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 19:46
George_George29-Apr-08 19:46 
GeneralRe: uncaught exception handlers Pin
PIEBALDconsult27-Apr-08 4:43
mvePIEBALDconsult27-Apr-08 4:43 
GeneralRe: uncaught exception handlers Pin
George_George29-Apr-08 2:29
George_George29-Apr-08 2:29 
Generalasynchronous function call Pin
George_George26-Apr-08 4:34
George_George26-Apr-08 4:34 
GeneralRe: asynchronous function call Pin
Spacix One26-Apr-08 6:01
Spacix One26-Apr-08 6:01 
GeneralRe: asynchronous function call Pin
George_George26-Apr-08 21:31
George_George26-Apr-08 21:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.