0

I have this example from C# 5 in a Nutshell book, by Albahari. The author states that 'unlike threads, tasks propagate exception'. However, below, "Null" is never written on the console. This is rather confusing, example code below

Task task = Task.Run(() => { throw new NullReferenceException("null"); });        
try
{
     task.Wait();
}
catch (AggregateException aex)
{
    if(aex.InnerException is NullReferenceException)
        Console.WriteLine("Null");
    else
    {
        throw;
    }
}

UPDATE:

Screenshot below

enter image description here This is the complete snippet, so nothing is missing. Any help greatly appreciated. Thanks

LePrinceDeDhump
  • 436
  • 6
  • 18
  • Try putting the `Task task = Task.Run(...)` inside the `try {...}` block as well? – Serge Aug 18 '14 at 17:47
  • What do you mean? You throw an exception, the exception is caught and you print to the console instead. `null` is printed just fine here. – Jeroen Vannevel Aug 18 '14 at 17:47
  • Asked many times... Search SO... – L.B Aug 18 '14 at 17:47
  • 1
    no , the author says that " if the code in task thrown an unhandled exceptin, that exception is automatically rethrown to whoever calls Wait()" ... but my VS breaks and points to the first line complaining that the exception was unhandled – LePrinceDeDhump Aug 18 '14 at 17:49
  • FYI your code doesn't compile. You're missing `()` after `NullReferenceException`. Also, I am unable to reproduce your issue: https://dotnetfiddle.net/R14s9B – tnw Aug 18 '14 at 17:49
  • It works when I run the sample...minus the slight compile error when throwing NullReferenceException – Dismissile Aug 18 '14 at 17:49
  • 2
    Have you tried continuing execution? VS might break and show the exception, but then proceeds as expected. – Douglas Aug 18 '14 at 17:51
  • I added a screenshot now , please see the complete code and the error – LePrinceDeDhump Aug 18 '14 at 17:51
  • 1
    I agree with @Douglas. You probably have Visual Studio set up to break whenever an exception is thrown, and not just when an exception is unhandled. – Dismissile Aug 18 '14 at 17:51
  • 1
    Isn't your task code done even before you enter `try`? – MarcinJuraszek Aug 18 '14 at 17:51
  • @MarcinJuraszek Perhaps, it'll depend on when the operation is actually scheduled. But it doesn't matter whether the exception occurs before or after it is waited on, both will be observed identically. – Servy Aug 18 '14 at 17:53
  • well..the example is from the book, and I do see that this is the only thing task ever does but still, exception should be propagated to the wait :( – LePrinceDeDhump Aug 18 '14 at 17:55
  • @LePrinceDeDhump Press F5 and it will write a `null` to Console. – L.B Aug 18 '14 at 17:55
  • @Dismissile: It happens even when set to break on user-unhandled only. VS can't tell whether the exception will be handled outside the task. – Douglas Aug 18 '14 at 17:55
  • @LePrinceDeDhump: It *will* be propagated to the `Wait`. Just press 'Continue' in your Visual Studio. – Douglas Aug 18 '14 at 17:56
  • The code prints `Null` for me, when running this as a console app... – Jon Skeet Aug 18 '14 at 17:56
  • @L.B ohhhh..it did work when i pressed F5. Then I ran the exe itself in the bin folder, and no exception thrown, null gets printed on screen. so this proves that exception is indeed being handled by the task.wait block... :) thanks – LePrinceDeDhump Aug 18 '14 at 17:57
  • Related question (and answers): [Stop visual studio from breaking on exception in Tasks](http://stackoverflow.com/q/16921984/1149773) – Douglas Aug 18 '14 at 17:58

0 Answers0