如何处理Task.Factory.StartNew异常? [英] How to handle Task.Factory.StartNew exception?

查看:719
本文介绍了如何处理Task.Factory.StartNew异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些奇怪的问题。任务抛出的异常总是不能独立处理,我该如何尝试处理它。



我尝试这样:



http://msdn.microsoft.com/en -us / library / dd997415%28v = vs.110%29.aspx

  private class MyCustomException:Exception 
{
public MyCustomException(String message):base(message)
{
}
}

public static void Main()
{
var task1 = Task.Factory.StartNew(()=>
{

抛出新的MyCustomException(我很糟糕,但不是太糟糕! );
});

try
{
task1.Wait();
}
catch(AggregateException ae)
{
//假设我们知道这个特殊异常发生了什么。
//重新引用其他内容。 AggregateException.Handle提供
//另一种表达方式。见后面的例子。
foreach(ae.InnerExceptions中的变量)
{
if(e是MyCustomException)
{
Console.WriteLine(e.Message);
}
else
{
throw;
}
}

}
Console.Read();
}

this:



http://dotnetcodr.com/2014/02/11/exception-handling-in-the-net-task-parallel-library-with-c-the-basics/



this:



http://blogs.msdn.com/b/pfxteam/archive/2010/08/06/10046819.aspx



this:

  var task = Task.Factory.StartNew(()=> this.InitializeViewModel myViewModel)); 
task.ContinueWith(o => MyErrorHandler(task.Exception),TaskContinuationOptions.OnlyOnFaulted);

并在StackOverflow上检查了很多其他类似的问题。但它始终是一样的 - 异常不处理。这些原始代码片段没有处理!我认为这里有一些魔法...
我工作在.Net框架4.0



同时,处理针对我的异常的单一方法是: p>

  Task.Factory.StartNew(()=> 
{
try
{
//做某事抛出异常
}
catch(异常)
{

}
});


解决方案

如果在Visual Studio中运行该示例代码,确实会收到消息 MyCustomException未被用户代码处理,Visual Studio将在该行中断。



并不意味着您的例外是真的未处理。这只是意味着,默认情况下,Visual Studio会中断在任务之内未处理的异常。您可以通过在不调试的情况下运行应用程序(Ctrl-F5)来验证这一点;您将注意到,您的异常按预期处理。



此问题在以下SO问题中有更详细的描述:




I have some strange problem here. Exception thrown by Task always not handled independently how can I try to handle it.

I try this:

http://msdn.microsoft.com/en-us/library/dd997415%28v=vs.110%29.aspx

private class MyCustomException : Exception
{
     public MyCustomException(String message) : base(message)
     {
     }
}

public static void Main()
{
   var task1 = Task.Factory.StartNew(() =>
   {

      throw new MyCustomException("I'm bad, but not too bad!");
   });

   try
   {
      task1.Wait();
   }
   catch (AggregateException ae)
   {
       // Assume we know what's going on with this particular exception. 
       // Rethrow anything else. AggregateException.Handle provides 
       // another way to express this. See later example. 
       foreach (var e in ae.InnerExceptions)
       {
           if (e is MyCustomException)
           {
               Console.WriteLine(e.Message);
           }
           else
           {
               throw;
           }
       }

    }
    Console.Read();
}

this:

http://dotnetcodr.com/2014/02/11/exception-handling-in-the-net-task-parallel-library-with-c-the-basics/

this:

http://blogs.msdn.com/b/pfxteam/archive/2010/08/06/10046819.aspx

this:

var task = Task.Factory.StartNew(() => this.InitializeViewModel(myViewModel));
task.ContinueWith(o => MyErrorHandler(task.Exception), TaskContinuationOptions.OnlyOnFaulted);

and check a lot of other similar questions on StackOverflow. But It is always the same - exception is not handled. It is not handled on these primitive code snippets! I think it is some magic here... I work on .Net Framework 4.0

Meanwhile single way to handle exception that works for me is:

        Task.Factory.StartNew(() =>
        {
            try
            {
                //do something that thrown exception
            }
            catch (Exception)
            {

            }
        });

解决方案

If you run that example code in Visual Studio, you will indeed get the message MyCustomException was unhandled by user code and Visual Studio will break in that line.

That does not mean that your exception is really unhandled. It just means that, by default, Visual Studio breaks on exceptions which are not handled inside the Task. You can verify this by running your application without debugging (Ctrl-F5); you will note that your exception gets handled as expected.

This issue is described in more detail in the following SO question:

这篇关于如何处理Task.Factory.StartNew异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆