5

According to some articles and blogs a code like the following one should lead to an exception in .NET 4

static void Main(string[] args)
    {
        Task.Factory.StartNew(() => { throw new Exception(); });
        Thread.Sleep(1000);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Console.WriteLine("Completed"); 
    }

Expected exception:

Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.

But it doesn`t. .NET 4 applications on my PC behave like .NET 4.5:

  • they don`t throw that aggregate exception by default
  • they detect the following setting in the configuration file:

< ThrowUnobservedTaskExceptions enabled="true"/>

Looks like .NET 4 has been patched to get the same behavior that .NET 4.5 has. It it true or I have some troubles with my configuration? Or any .NET4 app (not targeting 4.5) will behave that way if 4.5 is installed? Thanks in advance.

TDenis
  • 167
  • 1
  • 10

1 Answers1

7

My guess is that you're actually running on .NET 4.5. Bear in mind that .NET 4.5 is effectively installed over the top of .NET 4. Even if your application is targeted at .NET 4, if the user has installed .NET 4.5 you'll get the new behaviour.

It's entirely possible for a user to genuinely only have .NET 4 installed though...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks. Maybe it worth mentioning here (would be great): http://msmvps.com/blogs/jon_skeet/archive/2011/06/22/eduasync-part-12-observing-all-exceptions.aspx – TDenis Nov 13 '13 at 17:37
  • Is it possible to run v4.0 on a machine that has 4.5 installed (for testing purposes)? – TDenis Nov 13 '13 at 17:42
  • @TDenis: I don't *know* of any way of doing so, no. I'll see about editing the Eduasync entry... I don't typically go back to edit blog posts over two years old, but we'll see... – Jon Skeet Nov 13 '13 at 17:57
  • It`s interesting that Silverlight also behaves like v4.5 (and it has no app.config to reconfigure the behavior). – TDenis Nov 14 '13 at 05:16
  • @TDenis: Silverlight in general, or Silverlight on a machine with 4.5 installed? I don't know how much is shared between them. – Jon Skeet Nov 14 '13 at 06:24
  • Silverlight 5 on a machine with 4.5 installed. I`ll find a machine without 4.5 and post an update. – TDenis Nov 14 '13 at 18:52
  • Silverlight in general. – TDenis Nov 18 '13 at 17:02
  • @TDenis: Interesting - that does surprise me. – Jon Skeet Nov 18 '13 at 17:05
  • I've just noticed this same behaviour. However adding a Console.WriteLine(".Net Framework Version {0}", Environment.Version); prints out: .Net Framework Version 4.0.30319.18408 which suggests it IS running on .NET 4 (and not .NET 4.5). – Darragh Sep 22 '14 at 15:10
  • @Darragh: No, because `Environment.Version` gives the CLR version, not the framework version, and .NET 4 and .NET 4.5.x effectively use the same CLR. (There are various minor version updates, but you won't see a CLR version number of 4.5.x.x.) – Jon Skeet Sep 22 '14 at 15:11
  • Thanks. I just noticed that Environment.Version is indeed the same when I re-targeted my application at .NET 4.5. – Darragh Sep 22 '14 at 15:17