컴파일러 오류 CS1626

업데이트: 2007년 11월

오류 메시지

catch 절이 포함된 try 블록의 본문에서는 값을 생성할 수 없습니다.
Cannot yield a value in the body of a try block with a catch clause

try 블록과 관련된 catch 절이 있으면 try 블록에서 yield 문을 사용할 수 없습니다. 이 오류가 발생하지 않도록 하려면 yield 문을 catch 절의 바깥쪽으로 이동합니다.

다음 샘플에서는 CS1626 오류가 발생하는 경우를 보여 줍니다.

// CS1626.cs
using System.Collections;

class C : IEnumerable
{
   public IEnumerator GetEnumerator()
   {
      try
      {
         yield return this;  // CS1626
      }
      catch
      {
        
      }
   }
}

public class CMain
{
   public static void Main() { }
}