Task::FromException<TResult> Method (Exception^)
Creates a Task<TResult> that's completed with a specified exception.
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.
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.
Available since 10
.NET Framework
Available since 4.6