3

I'm trying to catch an NullReferenceException which is going to be thrown by Task.Factory.StartNew method. I think it should be caught by 'try' statement with task.Wait() method. I also referred to Why is this exception not caught?, but have no idea. Would you share your wisdom?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Csharp_study
{
    class Program
    {
        static void Main(string[] args)
        {
            Task my_task = Task.Factory.StartNew(() => { throw null; });
            try
            {
                my_task.Wait();
            }

            catch (AggregateException exc)
            {
                exc.Handle((x) =>
                    {
                        Console.WriteLine(exc.InnerException.Message);
                        return true;
                    });
            }

            Console.ReadLine();
        }
    }
}
Community
  • 1
  • 1

2 Answers2

2

This behavior is due to VS's debugger and not your code.

If you're in debug mode and have Just My Code enabled (which is the default setting for most languages), turning it off should do the trick.

To disable the Just My Code feature, go to Tools > Options > Debugging > General and uncheck Just My Code checkbox.

If you're wondering what enabling Just My Code feature does, here's a excerpt from msdn.

Enable Just My Code
When this feature is enabled, the debugger displays and steps into user code ("My Code") only, ignoring system code and other code that is optimized or that does not have debugging symbols.

chaosifier
  • 2,666
  • 25
  • 39
  • 1
    Thanks. Actually, I found the answer 3 months ago by myself as I made a comment at below one. But I couldn't find a way to close the question. So I'm marking yours as the one. Have a nice day! – Bicycle-riding Dog Aug 26 '17 at 11:28
1

If you want to handle exception for a task , check if it is faulted. If it is not faulted continue with execution.

   static void Main(string[] args)
        {
            Task my_task = Task.Factory.StartNew(() => { throw null; });

            my_task.ContinueWith(x =>
            {

                if (my_task.IsFaulted)
                {
                    Console.WriteLine(my_task.Exception.Message);

                }
                else {
                    //Continue with Execution
                }
            });
        }

And the return true; is invalid in this case as method does not have a return type.

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • Hello. Thanks for your solution. This makes sense, but is there any other way I can handle the exception? like using conventional exception handling statement. I'm also referring to [MSDN](https://msdn.microsoft.com/en-us/library/system.aggregateexception(v=vs.110).aspx), but it also can catch the exception with Task.wait(). – Bicycle-riding Dog Apr 21 '17 at 18:55
  • Continuing from above, I also tried your solution, but still getting same exception saying 'Unhandled', and also it looks the code is not handling the exception. – Bicycle-riding Dog Apr 21 '17 at 20:00
  • When working with tasks you have to check the state if it is faulted and the code above just works fine. Do you have the complete code? – Simsons Apr 23 '17 at 01:30
  • @Simons/ Thanks for your help. Above one is a whole code. I tested that code on 2 PC with 2 different environments. / VS2010 + .NET 4.0 / VS2015 + .NET4.6 / – Bicycle-riding Dog Apr 23 '17 at 12:08
  • Solution found:http://stackoverflow.com/questions/19865523/why-cant-i-catch-an-exception-from-async-code I'm closing this question. – Bicycle-riding Dog May 17 '17 at 20:52