Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

<ThrowUnobservedTaskExceptions> Element

.NET Framework 4.6 and 4.5

Specifies whether unhandled task exceptions should terminate a running process.

<configuration> Element
  <runtime> Element
    <loadFromRemoteSources> Element

The following sections describe attributes, child elements, and parent elements.





Required attribute.

Specifies whether unhandled task exceptions should terminate the running process.

enabled Attribute




Does not terminate the running process for an unhandled task exception. This is the default.


Terminates the running process for an unhandled task exception.

Child Elements


Parent Elements




The root element in every configuration file used by the common language runtime and .NET Framework applications.


Contains information about runtime initialization options.

If an exception that is associated with a Task has not been observed, there is no Wait operation, the parent is not attached, and the Task.Exception property was not read the task exception is considered to be unobserved.

In the .NET Framework 4, by default, if a Task that has an unobserved exception is garbage collected, the finalizer throws an exception and terminates the process. The termination of the process is determined by the timing of garbage collection and finalization.

To make it easier for developers to write asynchronous code based on tasks, the .NET Framework 4.5 changes this default behavior for unobserved exceptions. Unobserved exceptions still cause the UnobservedTaskException event to be raised, but by default, the process does not terminate. Instead, the exception is ignored after the event is raised, regardless of whether an event handler observes the exception.

In the .NET Framework 4.5, you can use the <ThrowUnobservedTaskExceptions> element in an application configuration file to enable the .NET Framework 4 behavior of throwing an exception.

You can also specify the exception behavior in one of the following ways:

  • By setting the environment variable COMPlus_ThrowUnobservedTaskExceptions (set COMPlus_ThrowUnobservedTaskExceptions=1).

  • By setting the registry DWORD value ThrowUnobservedTaskExceptions = 1 in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework key.

The following example shows how to enable the throwing of exceptions in tasks by using an application configuration file.

        <ThrowUnobservedTaskExceptions enabled="true"/> 

The following example demonstrates how an unobserved exception is thrown from a task. The code must be run as a released program to work correctly.

using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;

//Use the following config settings to enable the throwing of unobserved exceptions. 
//  <startup> 
//    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
//  </startup> 
//  <runtime> 
//    <ThrowUnobservedTaskExceptions enabled="true"/>
//  </runtime> 

public class Example
    static void Main()
        Task.Run(() => { throw new InvalidOperationException("test"); });
        while (true)
© 2015 Microsoft