<分区>
我正在测试下面的方法,我意识到当我输入错误的文件名时,错误被捕获但是当我在指定的位置上没有元素时,IndexOutOfBounds 的错误使程序崩溃。后者也适用于转换错误。
private static IEnumerable<Thing> Initialize()
{
try
{
string[] things = System.IO.File.ReadAllLines("things.txt");
return things.Select(_ => new Thing
{
Name = _.Split(';')[0],
Id = Convert.ToInt32(_.Split(';')[0])
});
}
catch(Exception exception)
{
Console.WriteLine(exception.Message);
return new List<Thing>();
}
}
为什么尽管 catch 中有最一般的 Exception 类型,但有些错误仍未得到处理?它与 LINQ 表达式失败有关吗?如果是这样,我该如何强制捕获?