Why I cannot put yield inside try?
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/346365/why-cant-yield-return-appear-inside-a-try-block-with-a-catch">Why can’t yield return appear inside a try block with a catch?</a>  </p>
</blockquote>



<p>I want to ensure my application would not crash and try to put my method inside try, my function is <code>IEnumerable</code> return type and I cannot do that.</p>

<p>What are my options?</p>

<pre><code>public static IEnumerable&lt;Packet&gt; splitPacket(Packet packet, int numberOfFragments)
{
    IpV4Datagram ipV4Datagram = packet.Ethernet.IpV4;

    if (ipV4Datagram.Protocol == IpV4Protocol.Tcp || ipV4Datagram.Protocol == IpV4Protocol.Udp)
    {
        EthernetLayer ethernet = (EthernetLayer)packet.Ethernet.ExtractLayer();
        IpV4Layer ipV4Layer = (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer();
        DateTime packetTimestamp = packet.Timestamp;
        ipV4Layer.HeaderChecksum = null;
        PayloadLayer payload = (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); //extract the data
        int totalLength = payload.Length;
        int partialLength = totalLength / numberOfFragments; //split data into smaller segments
        partialLength = (partialLength / 8) * 8; //make sure it's divisible with 8 (http://en.wikipedia.org/wiki/IPv4#Fragmentation_and_reassembly)
        ushort offset = 0; //send one by one

        while (offset &lt; totalLength)
        {
            int fragmentLength = partialLength; //get length for this fragment
            IpV4FragmentationOptions options = IpV4FragmentationOptions.MoreFragments;

            if (offset + fragmentLength &gt;= totalLength) //is this the last fragment ? trim length if needed
            {
                options = IpV4FragmentationOptions.None;
                fragmentLength = totalLength - offset;
            }

            byte[] newBuffer = ipV4Datagram.Payload.ToArray(); //copy the actual data into a new buffer
            PayloadLayer newPayload = new PayloadLayer { Data = new Datagram(newBuffer, offset, fragmentLength) };
            ipV4Layer.Fragmentation = new IpV4Fragmentation(options, offset); //change IP layer fragmentation options
            yield return PacketBuilder.Build(packetTimestamp, ethernet, ipV4Layer, newPayload); //return
            offset += (ushort)fragmentLength; //next offset
        }
    }
}
</code></pre>
