C
C#10mo ago
%Anhangüera

❔ Reading file (.txt)

There is a static function that returns IEnumerable<int> of a file. Each line of that file has a number. So for each number I'm returning a yield return. But there's a problem, I want to use a try/catch block. But it doesn't compile: "Cannot yield a value in the body of a try block with a catch clause" So how can I catch an exception without the catch here? (this finally empty looks really ugly) and why I can't use yield return inside catch? :(
4 Replies
Angius
Angius10mo ago
What exception do you expect to catch here?
%Anhangüera
%Anhangüera10mo ago
well, in this code any but what if I need a more complex logic there? and maybe if the filePath is not a valid path, maybe it throws an exception
Angius
Angius10mo ago
Have only that in the try-catch Only start looping and yielding when you're sure everything's valid
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.