// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; namespace E { public class Program { public static void DoSomething() { throw new Exception("This is not really an exception"); } public static void TryThreeTimes() { int ntry = 0; while(true) { ntry++; try { DoSomething(); break; } catch (Exception ex) when(ntry < 3) { Console.WriteLine("Retrying: " + ex.Message); } } } public static void Main(string[] args) { try { TryThreeTimes(); } catch(Exception ex) { Console.WriteLine("Giving up on: " + ex.Message); } } } }