Throw statement is very useful to handle exception in a program. Throwing exception handle by the catch block.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace what_is_through_statement_in_c_sharp
{
public class throw_statement //create class
{
public void devide(double x ,double y)//create function for division
{
double div;
try
{
if (y == 0)
{
throw new Exception("Can not Devide by Zero:\n");// throw Exception
}
div =Convert.ToDouble( x / y);
Console.WriteLine("Division Result is :" + div);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());//call to Exception
}
}
}
class Program
{
static void Main(string[] args)
{
throw_statement ts = new throw_statement();//create object
int number1, number2;
Console.WriteLine("Enter First Number");
number1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number");
number2 = Convert.ToInt32(Console.ReadLine());
ts.devide(number1, number2); //call to function Divide()
}
}
}
Output


Join the conversation! Your thoughts help the community grow.