AsyncCommand Exception-Handling
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;
}
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-02 Msg 547, Level 16, State
2021-07-02 Win10操作系统升级到20H2的进程名字
2021-07-02 Using Spy++ in Visual Studio 2019
2021-07-02 How to print GETDATE() in SQL Server with milliseconds in time?
2020-07-02 NLog Tutorial
2020-07-02 Securing Azure CDN assets with token authentication
2020-07-02 How can I uninstall the language pack of .net 4.8?