Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0201 Throw exception

To throw exception we can use the throw statement. using System; using System.IO; class Test { static void Display(string name) { if (name == null) throw new ArgumentNullException("name"); Console.WriteLine(name); } static void Main() { try { Display(null); } catch (ArgumentNullException ex) { Console.WriteLine("Caught the exception"); } } } The output: Caught the exception