I created this code to see, but I'm assuming that the enumerable is not being lazily evaluated when I test it.
public void EnumerableExceptionTest_CanOnlyBeTestedByInspectingError()
{
var ints = GetIntsButReallyThrowError(); // error occurs here.
var y = 10;
var z = 11;
foreach(var i in ints) // error *could* be on this line, but not in my tests.
{
Console.WriteLine(i+y+z);
}
}
private IEnumerable<int> GetIntsButReallyThrowError()
{
var x = true;
if (x)
{
throw new Exception("Threw this on purpose.");
}
else
{
return new int[]{1,2,3};
}
}
Is it possible though the error would not occur until it is lazily evaluated, and thus that the error line number would be that of the foreach loop?
*Note, this may be useful to keep in mind while debugging if the methodname isn't on the line that the callstack says it's on. It doesn't necessarily mean that your code / pdbs are out of date :-P