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.

4

1 回答 1

7

我的猜测是您实际上是在 .NET 4.5 上运行的。请记住,.NET 4.5 有效地安装在 .NET 4 之上。即使您的应用程序针对 .NET 4,如果用户安装了 .NET 4.5,您将获得新的行为。

用户完全有可能真正只安装 .NET 4...

于 2013-11-13T17:31:14.767 回答