• AsyncCommand ExceptionHandling


    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

     
     
    问题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

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

    最后的方案是,不过如果是这样的话,应该直接用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;
            }
  • 相关阅读:
    关于晋升的5个建议
    不拘一格:网飞的自由与责任工作法
    博恩·崔西的人生管理课
    老板防止我上班摸鱼,给我装了个chrome插件
    彻底搞懂彻底搞懂事件驱动模型
    python 100 days
    不要懒惰地重复自己
    不要让“追求完美”阻碍你做决策
    Windows上使用Python Terminal(终端控制台) 打印日志带有特殊符号时显示不出来
    Python3 Mysql DBhelper封装
  • 原文地址:https://www.cnblogs.com/chucklu/p/16437480.html
Copyright © 2020-2023  润新知