Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Task::FromException<TResult> Method (Exception^)

.NET Framework (current version)
 

Creates a Task<TResult> that's completed with a specified exception.

Namespace:   System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

public:
generic<typename TResult>
static Task<TResult>^ FromException(
	Exception^ exception
)

Parameters

exception
Type: System::Exception^

The exception with which to complete the task.

Return Value

Type: System.Threading.Tasks::Task<TResult>^

The faulted task.

Type Parameters

TResult

The type of the result returned by the task.

This method creates a Task<TResult> object whose Status property is TaskStatus::Faulted and whose Exception property contains exception. The method is commonly used when you immediately know that the work that a task performs will throw an exception before executing a longer code path. The example provides an illustration.

The following example is a command-line utility that calculates the number of bytes in the files in each directory whose name is passed as a command-line argument. Rather than executing a longer code path that instantiates a FileInfo object and retrieves the value of its FileInfo::Length property for each file in the directory, the example simply calls the FromException<TResult>(Exception^) method to create a faulted task if a particular subdirectory does not exist.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.6
Return to top
Show: