Types of Exceptions and how to handle them in C# .Net

Any human cannot write 100% complete bug free code, sometimes users might give unrealistic data to the application, that might cause exceptions, some of the types of exceptions that should be commonly handled in .net are :


Example for NullReferenceException


        public static void handleNullException()
        {
            try
            {
                string name = null;
                int nameLength = name.Length;
            }
            catch (NullReferenceException ne)
            {
                Console.WriteLine("Exception : " + ne.Message);
            }
        }



Example for DivideByZeroException


        public static void handleDivideByZero()
        {
            try
            {
                int result = 20 / int.Parse("0");
            }
            catch (DivideByZeroException de)
            {
                Console.WriteLine("Exception : " + de.Message);
            }
        }


Example for FormatException 


        public static void handleFormatException()
        {
            try
            {
                int number = int.Parse("string");
            }
            catch (FormatException ce)
            {
                Console.WriteLine("Exception : " + ce.Message);
            }
        }


Example for InvalidCastException


        public static void handleCastException()
        {
            try
            {
                int a = 10;
                object data = "name";
                int b = (short)data;
            }
            catch (InvalidCastException ce)
            {
                Console.WriteLine("Exception : " + ce.Message);
            }
        }


Example for IndexOutOfRangeException


   public static void handleIndexOutofRange()
        {
            try
            {
                int[] series = { 2, 8, };

                for (int i = 0; i < 2; i++)
                {
                    int aa = series[i + 3];
                }
            }
            catch (IndexOutOfRangeException ex)
            {
                Console.WriteLine("Exception : " + ex.Message);
            }
        }


Handling Multiple Exceptions in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExecptionProj
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //functionlity here.
            }
            catch (InvalidCastException ce)
            {
                Console.WriteLine("functionality here");
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine("functionality here");
            }
            catch (IndexOutOfRangeException iore)
            {
                Console.WriteLine("functionality here");
            }
            catch (DivideByZeroException dbz)
            {
                Console.WriteLine("functionality here");
            }
            catch (OutOfMemoryException om)
            {
                Console.WriteLine("functionality here");
            }
        }
    }
}