ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

AsyncCommand Exception-Handling

2022-07-02 14:00:08  阅读:126  来源: 互联网

标签:thrown Exception AsyncCommand exception Task Handling catch async method


AsyncCommand Exception-Handling

问题

I'm using an AsyncCommand to execute something like this:

C#
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

T372965.zip     问题2

Hello Michael,

thanks for the reply. Change your example to:

C#
} catch (Exception e)  
       {  
           throw;  
       }  
 

or just remove the catch completely.

I'm not shure if I'm wrong, but I would be thinking that this should lead to a unhandled exception in my application. When I'm starting a Task and don't handle it's exception it get's thrown as soon as the task gets disposed. Am I wrong?

Cheers,
Manuel

 

回答

Hello,
If an async method is awaited, the exception is thrown in code that awaits it. Unhandled exceptions behave this way only if the method's returning type is void. So, if you need to throw an unhandled exception, you need to use the void returning type. I also suggest you review the following articles where a similar question was discussed in the context of the C#/WPF platform:
1) Catch unhandled exceptions from async;
2) C# 5 Async Exception Handling;
3) Catch an exception thrown by an async method.

Thanks,
Kirill

 

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

T372965_Modified.zip

 

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).

C#
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.

C#
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().

 

标签:thrown,Exception,AsyncCommand,exception,Task,Handling,catch,async,method
来源: https://www.cnblogs.com/chucklu/p/16437480.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有