How to Throw an Exception in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

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");
            }
        }

    } 
}
Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

Newsletter Signup

Subscribe to our weekly newsletter below and never miss the latest product or an exclusive offer.

%d bloggers like this: