Tech Off Post

Single Post Permalink

View Thread: Ideas for C# 5.0
  • User profile image
    Curt Nichols

    exoteric said:

    Open question. Should there be special support for this scenario?


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace Test
        class Program
            static void Main(string[] args)
                var s1 = Enumerable.Range(1, 10);
                var s2 = s1.Select(x => x - 10);
                var s3 = Enumerable.Empty<int>();
                    s3 = s1.Zip(s2, (a, b) => a / b);
                catch (DivideByZeroException) // we'd like to catch the insect here
                    Console.WriteLine("got it");
                foreach (var x in s3) // but this is where it shows itself


    It's quite easy to build extension methods to catch and handle exceptions and chain these on a LINQ expression. It just doesn't follow the normal syntax for dealing with exceptions.



    Hmm, well, that's the framework, not C#, but I think the issue here is accepting how enumerables work. If you really need to know right now that nothing will cause the enumerable to throw then do something to materialize it right now, like s1.Zip(...).ToArray(). Alternatively, because you know where the exception can happen just put your catch in the right place. Smiley