Reasons to avoid async voidaside, is it possible to catch the following error from calling the code?
private static Action ErrorAction
{
get
{
return async () =>
{
await Task.Delay(0);
throw new NotImplementedException();
};
}
}
For example, in both of the following cases, I would like to get an exception, but both tests fail:
[Test]
public void SelfContainedExampleTryCatch()
{
List<Exception> errors = new List<Exception>();
try
{
ErrorAction();
}
catch (Exception ex)
{
errors.Add(ex);
}
errors.Count().Should().Be(1);
}
[Test]
public void SelfContainedExampleContinueWith()
{
List<Exception> errors = new List<Exception>();
var task = Task.Factory.StartNew(ErrorAction);
task.ContinueWith(t =>
{
errors.Add(t.Exception);
}, TaskContinuationOptions.OnlyOnFaulted);
task.Wait();
errors.Count().Should().Be(1);
}
I know I could use a method with a signature async Task; but, in particular, it is that you need to process the argument of the asynchronous code of the delegate assigned to Action, which I need to process, and it is almost impossible to catch globally. If possible, a solution common to synchronous and asynchronous actions would be ideal.
I hope there is a simple solution that I missed, but so far I have only headed a lot of dead ends and had (possibly wrongly) concluded that this is not possible. Any help (or wasting time saving) will be appreciated!