In this article we will learn what throw statement is in C # or what is the functionality of throw statement or How to use throw statement C sharp or how to try catch with throw statement in c sharp with example.
The Throw statement is used for throwing exception in c# program. The throwing exception is handled by try catch block. The throw statement is contained within a catch block. An easy example is mentioned below in a c# program that demonstrate the throw statement very clearly.
Exception handling tutorial:
static void Main(string args)
Console.WriteLine("Enter Your Name");
Name = Console.ReadLine();
Console.WriteLine("Enter Your Age");
age = Convert.ToInt32(Console.ReadLine());
if (age <18)
throw new Exception("you are not eligible for vote\n\n");
catch (Exception e)
Console.WriteLine("Error : " + e.ToString());
When you will execute this code, and input 17 for age then you will get the you are not eligible for vote that is raised by throw statements.
I hope this will help for you.