Say I write a linq query for info about the hidden files on my C drive:
var q2 =
from c in Directory.EnumerateFiles("C:\\")
where Regex.IsMatch(c, @"^C:\\\.")
select c;
var q3 =
from c in q2
let info = new
{
File = c,
FileSecurity = File.GetAccessControl(c),
FileAttributes = File.GetAttributes(c),
CreatedUTC = File.GetCreationTimeUtc(c),
AccessedUTC = File.GetLastAccessTimeUtc(c),
ModifiedUTC = File.GetLastWriteTimeUtc(c)
}
select info;
The above is an example of logic that might want to keep going when an exception is caught, but I don’t know how to get that to happen in this style of C# programming (or if its possible).
Conceptually I want some kind of “continue” statement that can be put in the body of a catch block surrounding a LINQ query.
try
{
// LINQ QUERY
}
catch(Exception ex)
{
Logger.Log("error...");
continue;
}
Due to the lazy nature of LINQ you might need to
try/catchwhen you start consuming the iterator and not when building the query:The Directory.EnumerateFiles simply returns an
IEnumerable<string>and no evaluation happens until you start iterating over it.UPDATE:
To avoid breaking out of the loop if an exception is thrown you could do the following: