AsyncCommand Exception-Handling
问题
I'm using an AsyncCommand to execute something like this:
private async Task Refresh()
{
try
{
await somethingAsync();
}
catch (Exception ex)
{
if (ex is SessionExpiredException) throw;
.......
}
}
But the thrown exception seems to dissapear. Is this because the Command has to be collected to get the exception thrown? I did found an identical request here: https://www.devexpress.com/Support/Center/Question/Details/T144566 - But I've got no idea how to solve this…
Cheers,
Manuel
回答1
Hello Manuel,
I have tested the provided code snippet on my side and it works properly. Would you review the attached sample project and clarify if I missed anything? I'm looking forward to your reply.
Thanks,
Michael
Hello,
I've modified the sample project to demonstrate how you can use a method returning the "void" type. Please take a moment to review it.
Thanks,
Kirill
Hi, thanks for the reply.
When I have to create a new Task in an async command to catch exceptions - what's the reason for using async command?
回答
Hi Manuel,
This behavior is .NET platform specific.
There is no problem to catch an exception within an async method.
However, an unhandled exception thrown within an async method won't stop the application.
You can check it with the following code (without using our AsyncCommand).
void Go() {
Refresh().ContinueWith((t) => { });
}
async Task Refresh() {
try {
await Task.Delay(2000);
throw new Exception("Test exception");
}
catch {
Console.WriteLine("Caught");
throw;
}
}
However, if you invoke the Refresh() method with await, the exception will be forwarded to Go() and can be caught there.
async void Go() {
try {
await Refresh();
}
catch {
Console.WriteLine("Caught");
}
}
That's why, Kirill recommended you use an inner Task to handle and re-raise your exception within the command.
As for AsyncCommand, it doesn't contain any exception handling logic. It just invokes the command delegate and then does finalizing actions within ContinueWith().
最后的方案是,不过如果是这样的话,应该直接用DelegateCommand
public AsyncCommand(Func<Task> executeMethod);
public DelegateCommand(Action executeMethod);
使用dnSpy反编译代码之后,里面执行方法的核心代码,和ContinueWith有关
private Task ExecuteCore(T parameter)
{
this.executeTask = this.executeMethod(parameter).ContinueWith(delegate(Task x)
{
this.IsExecuting = false;
this.ShouldCancel = false;
if (x.IsFaulted)
{
throw x.Exception.InnerException;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
return this.executeTask;
}