// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; public class Guard11 { public static void M1(string? v) { if(v == null) { throw new ArgumentNullException("v"); } } public static void M2(int v) { if(v == 0) { throw new ArgumentException("v=0"); } if(v < 0) { throw new ArgumentOutOfRangeException("v"); } } public static void Test1(string? v) { try { M1(v); Console.WriteLine("OK"); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static void Test2(int v) { try { M2(v); Console.WriteLine("OK"); } catch(Exception ex) { Console.WriteLine(ex.Message); } } public static void Main(string[] args) { Test1("ABC"); Test1(null); Test2(1); Test2(0); Test2(-1); } }