• 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;
            }
  • 相关阅读:
    基本二叉搜索树的第K小元素
    sklearn常见分类器(二分类模板)
    python图论包networks(最短路,最小生成树带包)
    PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
    PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
    Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题。 username/
    JavaScritpt的DOM初探之Node(一)
    怎样实现动态加入布局文件(避免 The specified child already has a parent的问题)
    Ubuntu 14.04下单节点Ceph安装(by quqi99)
    卡片游戏
  • 原文地址:https://www.cnblogs.com/chucklu/p/16437480.html
Copyright © 2020-2023  润新知