相关疑难解决方法(0)

捕获异步void方法抛出的异常

使用Microsoft for .NET的异步CTP,是否可以捕获调用方法中异步方法抛出的异常?

public async void Foo()
{
    var x = await DoSomethingAsync();

    /* Handle the result, but sometimes an exception might be thrown.
       For example, DoSomethingAsync gets data from the network
       and the data is invalid... a ProtocolException might be thrown. */
}

public void DoFoo()
{
    try
    {
        Foo();
    }
    catch (ProtocolException ex)
    {
          /* The exception will never be caught.
             Instead when in debug mode, VS2010 will warn and continue.
             The deployed the app will simply crash. …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous exception-handling task-parallel-library async-await

256
推荐指数
5
解决办法
19万
查看次数