C# program for Throw Statement

This is the simple example of throw Statement.
In this program here we use the Throw statement which throws the exception if the user will enter the value which meets the condition which has been set for the throw block.

There are mainly three block on which this system works.
1) Throw : This is the initial stage of the program.
In this block the program will throw the exception error.

2) Try : This is the second stage of the program.
In this block the program will print the statement which define by the programmer, if the condition will be will be true.

3) Catch : This is the final block of the program.
In this block the program will print the error and finally it will stop the execution of the program.

This statement is use to prevent the program from the shutdown of a program, whether the condition will be true than it will give error in the runtime.

NOTE: There also a “Finally” block which has been design to give output whether the upper blocks will be true or false by default it will execute, it did not depend on any other block as we seen in the previous blocks.

ENJOY CODING!!!
___________________________________________________________________________________

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Throw_Statement
{
    class PostSlush
    {
        static void Main(string[] args)
        {
            int a, b, Ans;
            Console.WriteLine("Enter First Number");
            a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter Second Number");
            b = Convert.ToInt32(Console.ReadLine());
            try
            {
                if (b == 0)
                {
                    throw new Exception("Can’t Divide by Zero Exceptionnn");
                }
                Ans = a / b;
                Console.WriteLine("{0} / {1} = {2}", a, b, Ans);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : " + e.ToString());
            }
                  Console.ReadLine();
        }
    }
}

 

Output : 

 If the condition will be false the output will be as follows :

 But, if the condition will be true than it will give this output :

Share Your Comments
Previous Smart Contact Lens for measuring glucose levels in tears Google Announced
Next Multi Boot your Nexus 5 and 7....

About author

You might also like