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:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace throw_statement
    class Program
        static void Main(string[] args)
            string Name;
            int age;
            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");
                Console.WriteLine("Name:{0}", Name);
                Console.WriteLine("Age:{0}", age);
            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.


What throw statement is in C #

I hope this will help for you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like