C # Async Exception Wrapping

I played with asynchronous calls in .NET 4.6.1, and I wonder what the correct way to throw errors is to implement an interface interface that expects an asynchronous method, but actually synchronous. For instance:

public interface ISomeInterface
{
    Task ExecuteAsync();
}

public class SomeClass : ISomeInterface
{
    public Task ExecuteAsync()
    {
        return Task.FromException(new Exception());
    }
}

I found Task.FromException here .

So this .NET 4.6 still seems to advise wrapping exceptions. However, I could just write the following code:

public class SomeClass : ISomeInterface
{
    public Task ExecuteAsync()
    {
        throw new Exception();
    }
}

, try/catch, Exception, , , , Task.FromException , , , ( ). , , , , . - async, -, ?

, async . , ?

+4
1

- , , .

, , Task.

, , :

var task = obj1.ExecuteAsync();
await task;

, ExecuteAsync. , , await ed. await ed , (, Task.WhenAll ).

( ) API- . async - API . , , API-, , , .

- , . , , . LINQ to Objects, , , .

Task. Task.

+1

Source: https://habr.com/ru/post/1661048/


All Articles