我可以在 LINQ 投影中捕获异常吗?
假设我编写了一个 linq 查询,以获取有关 C 驱动器上隐藏文件的信息:
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;
上面是一个逻辑示例,当捕获异常时,该逻辑可能希望继续执行,但我不知道如何在本例中实现这一点C# 编程风格(或者如果可能的话)。
从概念上讲,我想要某种“继续”语句,可以将其放入围绕 LINQ 查询的 catch 块的主体中。
try
{
// LINQ QUERY
}
catch(Exception ex)
{
Logger.Log("error...");
continue;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 LINQ 的惰性本质,您可能需要在开始使用迭代器时而不是在构建查询时进行尝试/捕获:
Directory.EnumerateFiles 仅返回一个
IEnumerable
并且在开始之前不会进行任何评估迭代它。更新:
为了避免在抛出异常时跳出循环,您可以执行以下操作:
Due to the lazy nature of LINQ you might need to
try/catch
when 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: