I have the following example: (please also read the comments in the code as this will make more sense)
public async Task<Task<Result>> MyAsyncMethod() { Task<Result> resultTask = await _mySender.PostAsync(); return resultTask;
PostAsync _ mySender looks like this:
public Task<Task<Result>> PostAsync() { Task<Result> result = GetSomeTask(); return result; }
The question arises:
Since I do not expect the actual Result in MyAsyncMethod , and if the PostAsync method PostAsync an exception, in what context does the exception occur and the exception is handled?
and
Is there a way to handle exceptions in my assembly?
I was surprised when I tried to change MyAsyncMethod to:
public async Task<Task<Result>> MyAsyncMethod() { try { Task<Result> resultTask = await _mySender.PostAsync(); return resultTask; } catch (MyCustomException ex) { } }
an exception was caught here, an event if there is no expected actual result. It happens that the result of PostAsync already available, and the exception is correctly selected in this context?
Is it possible to use ContinueWith to handle exceptions in the current class? For example:
public async Task<Task<Result>> MyAsyncMethod() { Task<Result> resultTask = await _mySender.PostAsync(); var exceptionHandlingTask = resultTask.ContinueWith(t => { handle(t.Exception)}, TaskContinuationOptions.OnlyOnFaulted); return resultTask; }
Dan dinu
source share