Changes in new versions / C# 5.0

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program {
    static Task<intLoadAsync(string name) {
        return Task.Run(() => {
            if (name == "bad"throw new InvalidOperationException("no page " +
                          name);
            return name.Length;
        });
    }

    // *** before: ***
    // a loop over callbacks turned into recursion, and the error arrived
    // where a try around the call could not catch it
    static void LoadAllOld(List<string> names, int index, int total,
                          Action<int> done) {
        if (index == names.Count) { done(total); return; }
        LoadAsync(names[index]).ContinueWith(t => {
            if (t.IsFaulted) {
                Console.WriteLine("skipped");
                LoadAllOld(names, index + 1, total, done);
                return;
            }
            LoadAllOld(names, index + 1, total + t.Result, done);
        });
    }

    // *** in version 5.0: ***
    // an ordinary loop and an ordinary try, although the work is not
    // finished at the moment await is reached
    static async Task<intLoadAllNew(List<string> names) {
        int total = 0;
        foreach (string name in names) {
            try {
                total += await LoadAsync(name);
            } catch (InvalidOperationException e) {
                Console.WriteLine("skipped: " + e.Message);
            }
        }
        return total;
    }

    static void Main() {
        List<string> names = new List<string> { "alpha""bad""beta" };
        LoadAllOld(names, 00, total => Console.WriteLine(total));
        Console.WriteLine(LoadAllNew(names).Result);

        // await inside catch and finally is still forbidden here; that came
        // only in C# 6
    }
}