Define exception variable in catch statement: DivideByZeroException : Predefined Exception « Language Basics « C# / CSharp Tutorial






using System;

class MainClass
{
   static void Main()
   {
      try
      {
         int y = 0;
         y = 10/y; 
      } catch (DivideByZeroException e) {
         Console.WriteLine("Message: {0}", e.Message);
         Console.WriteLine("Source: {0}", e.Source);
         Console.WriteLine("Stack: {0}", e.StackTrace);
      }
   }
}
Message: Attempted to divide by zero.
Source: main
Stack:    at MainClass.Main()








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