CSharp - Forcing an Exception in a Sequential Query

Description

Forcing an Exception in a Sequential Query

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

class Program/*  w  ww  .j a v a 2  s.co m*/
{
    static void Main(string[] args)
    {
            string[] codeNames = { "Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
            
            // Parallel LINQ query
            IEnumerable<string> results = codeNames
                .Select(p => {
                    if (p == "Java")
                        throw new Exception(String.Format("Problem with President {0}", p));
                    return p;
                });
            
            
            try {
                foreach (string president in results) {
                    Console.WriteLine("Result: {0}", president);
                }
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }

    }
}

Related Topic