【问题标题】:Exception handling in tasks in c#c#任务中的异常处理
【发布时间】:2015-03-11 13:23:13
【问题描述】:

我在任务中遇到异常处理问题。我在 MSDN 上找到了任务中异常处理的页面,但是该示例对我不起作用...

我希望应该处理异常,但是如果我启动程序,我会收到未处理的异常错误。

谢谢。

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

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;

   namespace TaskExceptionTest
   {

class Program
{
    static string[] GetAllFiles(string str)
    {
        // Should throw an AccessDenied exception on Vista. 
        return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories);
    }
    static void HandleExceptions()
    {
        // Assume this is a user-entered string. 
        string path = @"C:\";

        // Use this line to throw UnauthorizedAccessException, which we handle.
        Task<string[]> task1 = Task<string[]>.Factory.StartNew(() => GetAllFiles(path));

        // Use this line to throw an exception that is not handled. 
        //Task task1 = Task.Factory.StartNew(() => { throw new IndexOutOfRangeException(); } ); 
        try
        {
            task1.Wait();
        }
        catch (AggregateException ae)
        {

            ae.Handle((x) =>
            {
                if (x is UnauthorizedAccessException) // This we know how to handle.
                {
                    Console.WriteLine("You do not have permission to access all folders in this path.");
                    Console.WriteLine("See your network administrator or try another path.");
                    return true;
                }
                return false; // Let anything else stop the application.
            });

        }

        Console.WriteLine("task1 has completed.");
    }

    static void Main(string[] args)
    {
        HandleExceptions();
    }
}

}

【问题讨论】:

  • “我收到一个未处理的异常错误。” - 有机会分享错误吗?
  • “未处理的异常错误” - 哪个异常?在哪一行?被抓到了吗?如果你按 F5 会发生什么?
  • UnauthorizedAccesException 在返回 System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories); 时未被用户代码处理
  • 您没有访问该目录的权限,这就是它首先被抛出的原因。
  • 是的,但稍后将在 task.wait 中处理。它会在那里被捕获,但我已经在 GetAllFiles 中得到了未处理的异常。我应该怎么做才能没有任何未处理的异常?

标签: c# exception-handling task


【解决方案1】:

返回false 就是重新抛出异常。如果您只想打印额外的错误信息,请将return false 替换为return true 并去掉if 块中的return。

ae.Handle((x) =>
        {
            if (x is UnauthorizedAccessException) // This we know how to handle.
            {
                Console.WriteLine("You do not have permission to access all folders in this path.");
                Console.WriteLine("See your network administrator or try another path.");
            }
            return true; 
        });

【讨论】:

  • 好的,我已经更正了那部分。 UnauthorizedAccesException 将在这里处理,但我在 GetAllFiles 中已经得到了未处理的异常。我应该怎么做才能在我的项目中没有任何未处理的异常?
  • 您需要在抛出异常时对其进行处理,您正在处理来自任务的聚合异常,这与您的代码编写方式是正确的。如果你不想从任务中抛出 UnauthorizedAccessException,你应该直接在任务中处理它。
  • 我已经尝试过了。我尝试在 GetAllFiles 中抛出异常,但我仍然在 GetAllFiles 中得到未处理的异常。 static string[] GetAllFiles(string str) { try { // Should throw an AccessDenied exception on Vista. return System.IO.Directory.GetFiles(str, "*.txt", System.IO.SearchOption.AllDirectories); } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
  • 2014-03-06
  • 1970-01-01
  • 2018-03-20
  • 2015-03-10
  • 1970-01-01
  • 2015-07-03
相关资源
最近更新 更多