简体   繁体   English

处理带有任务的异步方法中的异常的问题

[英]Problems with handling exceptions from async method with tasks

I hope you can help me with that problem. 希望您能帮我解决这个问题。

I have a method that does specific actions, for example, I am sending a HttpWebRequest. 我有一个执行特定操作的方法,例如,我正在发送HttpWebRequest。 There I can get a WebException , so I put it in a Try-Catch-block to rethrow exceptions for more specific exception messages. 在那里,我可以获得WebException ,因此我将其放在Try-Catch-block中,以将异常重新抛出以获取更具体的异常消息。

Like that: (This is in a method called doWebRequest ) 像这样:(这是在名为doWebRequest的方法中)

try 
{
   // HttpWebRequest here
}

catch (WebException ex)
{
  throw new WebException(String.Format("My special additional message {0}", ex.Message);
}

Well, so this works, when I call this function normally. 好吧,所以当我正常调用此函数时,此方法有效。 But now I want an async way to do this. 但是现在我想要一种异步的方式来做到这一点。 What I made to call this method in an async method: 我在异步方法中将此方法称为什么:

public void DoRequestAsync() 
{
   Task internalRequest = new Task(doWebRequest);
   internalRequest.ContinueWith(InternalUpdateSearchExceptionHandler, TaskContinuationOptions.OnlyOnFaulted);
   internalRequest.Start();
}

So this makes a new task and then calls the method doWebRequest async. 因此,这将产生一个新任务,然后调用方法doWebRequest async。 To handle errors now, because I know it is different on async, I made this handler, which you can also see in internalRequest.ContinueWith . 现在要处理错误,因为我知道异步处理是不同的,所以我做了这个处理程序,您也可以在internalRequest.ContinueWith中看到它。 Looks like that: 看起来像这样:

private void InternalUpdateSearchExceptionHandler(Task task)
{
  var ex = task.Exception;
  if (ex.InnerException is WebException)
  {
     if ((ex.InnerException as WebException).Status == WebExceptionStatus.ProtocolError)
     {
        throw new WebException(ex.InnerException.Message);
     }

     else
     {
        throw new Exception("There was no response from the server.");
     }
  }
}

But this is not executing any exceptions. 但这没有执行任何异常。 I don't know why. 我不知道为什么 At first I thought, this is because it cannot take the InnerException as a WebException, or would that work? 起初我以为是因为它不能将InnerException当作WebException,否则行得通吗? If not, please tell me what to do here. 如果没有,请告诉我在这里做什么。 But even when I throw an exception without any queries here, it did not throw any exceptions while debugging. 但是,即使我在这里抛出没有任何查询的异常时,在调试时也没有抛出任何异常。 Why is that? 这是为什么?

Help is appreciated. 感谢帮助。 When something is not clear, ask me. 当不清楚的时候,问我。 ;) ;)

Your continuation is throwing an exception, but when that continuation throws an exception all that happens is the Task that represents that continuation is marked as Faulted with the given exception as its Exception. 您的延续引发异常,但是当该延续引发异常时,所有发生的事情就是代表该延续的Task被标记为Faulted,给定的异常为其Exception。 You currently are ignoring the task created by calling ContinueWith , so there is nothing to observe this exception. 当前,您正在忽略通过调用ContinueWith创建的任务,因此没有任何可观察到的异常。

DoRequestAsync likely shouldn't be void ; DoRequestAsync可能不应该void ; rather it should return a Task , specifically the one created by calling ContinueWith , so that whoever calls this method can observe any exceptions thrown. 而是应该返回一个Task ,特别是通过调用ContinueWith创建的Task ,这样,调用此方法的任何人都可以观察到抛出的任何异常。

Also note that you could do this much more simply using async : 还要注意,您可以使用async来完成更多操作:

public async Task DoRequestAsync() 
{
    try 
    {
        var content = await new WebClient()
            .DownloadStringTaskAsync("address");
    }
    catch (WebException ex)
    {
      throw new WebException(String.Format("My special additional message {0}", ex.Message);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM