How to Throw an Exception in C# ?

To indicate and error which can be handled by the caller function , you can use the throw keyword to throw an exception .

For example , below is a sample code snippet demonstrating how to throw an exception in C#.

How to Throw an Exception in C# ?

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

namespace AbundantcodeConsole
{ 
    class Program
    {
        static void Main(string[] args)
        {
            int input = 0;
            if(input==0)
            {
                throw new ArgumentNullException("The value cannot be zero");
            }
        }

    } 
}
%d bloggers like this: