I have this piece of code:
try { var files = from folder in paths from file in Directory.EnumerateFiles(path, pattern, searchOption) select new Foo() { folder = folder, fileName = file }; Parallel.ForEach(files, new ParallelOptions { MaxDegreeOfParallelism = _maxDegreeOfParallelism }, currentFile => { DoWork(currentFile); }); } catch (Exception ex) { } When I have an exception in Directory.EnumerateFiles, I can't catch this exception in this piece of code. The exception is caught by the method that calls this snippet.
From Visual Studio, in debug mode, the exception is caught by Visual Studio (for example a DirectoryNotFoundException).
EnumerateFilesbefore the LINQ expression and wrap that call in a try/catch.files.ToList()before theParalleland then your catch would fire.foldertoDirectory.EnumerateFilesinstead ofpath?