Thursday 22 March 2012

Throwing Exception


//Program to throwing Exception Manually
using System;
class Program
{
static void Main()
{
int age=0;
try
{
Console.Write("Enter Age: ");
age=int.Parse(Console.ReadLine());
Console.WriteLine();

if(age<=0)
//throw is a keyword in c#.Net used to throw an exception.
throw new FormatException("Age Can not be less Than equal to zero");

if(age>100)
throw new FormatException("Invalid Range Of Age");
}
catch(Exception exp)
{
Console.WriteLine(exp.Message);
}
finally
{
Console.WriteLine("Entered Age: {0}",age);
}
}
}

No comments:

Post a Comment