3 Replies - 419 Views - Last Post: 26 July 2013 - 08:42 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

my try catch isn't catching my exception

Posted 26 July 2013 - 07:52 AM

I currently creating a demo app. this app is multithreaded. I don't understand why the exception isn't caught for one of the threads?

static void Main(string[] args)
        {
            newclass obj1 = new newclass();
            try
            {
                for (int a = 0; a < 5; a++)
                {
                    Thread thread = new Thread(obj1.DoStuff);
                    thread.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.ReadKey();
        }



 public class newclass
    {
        bool button = false;

        public void DoStuff()
        {

            var container = new UnityContainer().AddNewExtension<EnterpriseLibraryCoreExtension>();

            var exceptionmanager = container.Resolve<ExceptionManager>();

            exceptionmanager.Process(Increment, "Disposed");

        }

        public  void Increment()
        {
            object _lock = new object();

            lock (_lock)
            {
                if (button == false)
                {
                    button = true;
                    throw new ObjectDisposedException(typeof(baseclass).FullName);
                }

                Console.WriteLine("thread executed successfully");
            }

           
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: my try catch isn't catching my exception

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: my try catch isn't catching my exception

Posted 26 July 2013 - 07:55 AM

Sure - think about it... normally an application will process lines on a stack, right? If it hits an exception it will try to find the appropriate try/catch on the stack. When you multithread the process a whole new thread/stack is created. Errors, mostly, on that stack are not reported back to the original stack.
Was This Post Helpful? 2
  • +
  • -

#3 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: my try catch isn't catching my exception

Posted 26 July 2013 - 08:36 AM

Thanks Modi, that helped.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,019
  • Joined: 12-June 08

Re: my try catch isn't catching my exception

Posted 26 July 2013 - 08:42 AM

Side note - if you haven't looked into this yet I would also dabble in 'tasks'. They are more light weight thread-ish options when compared to threads and have a wonderful set of parallel processing options available to them

Some reading:
http://channel9.msdn...-Theads-v-Tasks
http://stevenhollidg...-threading.html
http://software.inte...tead-of-threads
http://www.dreaminco...arallelfor-101/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1