Throwing Exceptions: ArgumentNullException : Predefined Exception « Language Basics « C# / CSharp Tutorial






using System;

class MainClass
{
   static void Main()
   {
      string arg = null;
      
      try
      {
         if (arg == null)
         {
            ArgumentNullException MyEx = new ArgumentNullException();
            throw MyEx;
         }
      }
      catch (ArgumentNullException e)
      {
         Console.WriteLine("Message: {0}", e.Message);
      }
   }
}
Message: Value cannot be null.








1.23.Predefined Exception
1.23.1.Use the NullReferenceException.
1.23.2.Throwing Exceptions: ArgumentNullException
1.23.3.Handle ArgumentOutOfRangeException
1.23.4.Check OverflowException for long
1.23.5.Generate an index out-of-bounds exception
1.23.6.Define exception variable in catch statement: DivideByZeroException