Await in catch block

前端 未结 9 1497
醉梦人生
醉梦人生 2020-11-28 11:14

I have the following code:

WebClient wc = new WebClient();
string result;
try
{
  result = await wc.DownloadStringTaskAsync( new Uri( \"http://badurl\" ) );
         


        
9条回答
  •  心在旅途
    2020-11-28 11:32

    This seems to work.

            WebClient wc = new WebClient();
            string result;
            Task downloadTask = wc.DownloadStringTaskAsync(new Uri("http://badurl"));
            downloadTask = downloadTask.ContinueWith(
                t => {
                    return wc.DownloadStringTaskAsync(new Uri("http://google.com/")).Result;
                }, TaskContinuationOptions.OnlyOnFaulted);
            result = await downloadTask;
    

提交回复
热议问题