Alex Rivera | Logout

Exceptions in multithreaded application.

Asked 2011-04-06T16:08:13.740
10

I have heard from a very discerning person that an exception being thrown (and not caught) in a thread is being propagated to the parent thread. Is that true? I have tried something like this but couldn't catch the exception in the creating thread.

    static void Main(string[] args)
    {
        ParameterizedThreadStart pts = 
           new ParameterizedThreadStart(ThreadMethod);
        try
        {
            Thread t = new Thread(pts);
            t.Start(new object());
            Console.ReadLine();
        }
        catch (Exception ex) //the exception is not caught
        {
            Debugger.Break();
        }
    }


    static void ThreadMethod(object @object)
    {
        Thread.Sleep(2000);
        throw new IndexOutOfRangeException();
        Thread.CurrentThread.Abort();
    }
Edit
Report

1 Answer

1

Exception propagate upwards in the call-stack.

If you start a new thread from a certain method, it will propagate upwards until it gets to that method.

If that method doesn't catch it, you'll get a runtime error that there's an uncaught exception.

answered 2011-04-06T16:16:49.787

Your Answer