Exception handling for yield return <expression> statement? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Exception handling for yield return <expression> statement?

From C# Documentation (https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx) - A yield return statement can't be located in a try-catch block. - A yield return statement can be located in the try block of a try-finally statement. I am really curious about how to do exception handling for the yield return statement. Can anyone pls provide a good example or explanation on how to achieve this? Also, if an exception occurs during an iteration, will the next iteration continues?

25th Feb 2017, 6:13 AM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
1 Answer
+ 2
You CAN however add yield break to the catch block. "A yield break statement can be located in a try block or a catch block but not a finally block." So the solution is to move the yield statements outside of your try catch blocks and add yield break to catch. There's a great article on this at http://jacksondunstan.com/articles/3038
7th Mar 2017, 10:52 AM
Anthony Vanover
Anthony Vanover - avatar