Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中任务的异常处理#_C#_Exception Handling_Task - Fatal编程技术网

C# c语言中任务的异常处理#

C# c语言中任务的异常处理#,c#,exception-handling,task,C#,Exception Handling,Task,我在任务中遇到异常处理问题。我在MSDN上找到了任务中异常处理的页面,但是这个示例对我不适用 我希望应该处理异常,但是如果我启动程序,我会得到一个未处理的异常错误 谢谢 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 命名空间TaskExceptionTest { 班级计划 { 静态字符串[]GetAllFiles(字符串str) { //应在Vista上引发AccessD

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


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

谢谢

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间TaskExceptionTest
{
班级计划
{
静态字符串[]GetAllFiles(字符串str)
{
//应在Vista上引发AccessDenied异常。
返回System.IO.Directory.GetFiles(str,“*.txt”,System.IO.SearchOption.AllDirectories);
}
静态void HandleExceptions()
{
//假设这是用户输入的字符串。
字符串路径=@“C:\”;
//使用此行抛出UnauthorizedAccessException,我们将处理它。
Task task1=Task.Factory.StartNew(()=>GetAllFiles(path));
//使用此行抛出未处理的异常。
//Task task1=Task.Factory.StartNew(()=>{throw new IndexOutOfRangeException();});
尝试
{
task1.等待();
}
捕获(聚合异常ae)
{
ae.手柄((x)=>
{
if(x是UnauthorizedAccessException)//我们知道如何处理这个问题。
{
WriteLine(“您没有访问此路径中所有文件夹的权限。”);
WriteLine(“请与网络管理员联系或尝试其他路径”);
返回true;
}
return false;//让其他任何操作停止应用程序。
});
}
Console.WriteLine(“任务1已完成”);
}
静态void Main(字符串[]参数)
{
HandleExceptions();
}
}

}

返回的
false
正在重新引发异常。如果您只想打印额外的错误信息,则将
返回假
替换为
返回真
,并清除
If
块中的
返回

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; 
        });

“我收到一个未处理的异常错误。”-有没有可能共享该错误?“一个未处理的异常错误”-哪个异常?在哪一行?它被抓住了吗?如果您点击F5会发生什么情况?用户代码在返回System.IO.Directory.GetFiles(str,“*.txt”,System.IO.SearchOption.AllDirectory)时未处理UnauthorizedAccesException;您没有访问该目录的权限,这就是为什么会首先抛出该目录。是的,但稍后将在task.wait处理该目录。它将在那里被捕获,但我在GetAllFiles中已经得到了未处理的异常。我应该怎么做才能没有任何未处理的异常?好的,我已经纠正了那个部分。UnauthorizedAccesException将在此处处理,但我在GetAllFiles中已经得到未处理的异常。我应该怎么做才能在我的项目中没有任何未处理的异常?您需要在抛出异常时处理该异常,您正在处理任务中的聚合异常,这是正确的代码编写方式。如果你不想从任务中抛出UnauthorizedAccessException,你应该直接在任务中处理它。我已经试过了。我尝试在GetAllFiles中抛出异常,但仍然在GetAllFiles中得到未处理的异常<代码>静态字符串[]GetAllFiles(字符串str){try{//应在Vista.return System.IO.Directory.GetFiles(str,“*.txt”,System.IO.SearchOption.AllDirectories)上引发AccessDenied异常;}catch(异常e){Console.WriteLine(e.Message);throw;}}
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; 
        });