7/15/2010

C# Exception Handling (User Defined Exception)



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExceptionHandling
{
    class pos_val : Exception
    {
        
        public pos_val(string msg)
            : base(msg)
        {
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter Integer Value");
                int a = int.Parse(Console.ReadLine());

                if (a < 0)
                    throw new pos_val("Enter Positive Numbers Only");
                else
                    Console.WriteLine("Value: " + a);
            }
            catch (pos_val e)
            {
                Console.WriteLine(e.Message);
            }

            finally
            {
                Console.WriteLine("End of a Program");
            }


        }
    }
}

No comments:

Post a Comment