MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

Exception handling at its best

Write a c# program to add two numbers. This may sound very simple, but the catch, is - The program should not break, and always should give meaningful error messages when exception conditions occur. For example, the following error conditions should be handled in your program. Also, the program should run as long as the user wants it to run.
1. If the user enters "Ten" instead of 10, the program should let the user know only numbers can be added.
2. If the user, enters a very large number, the program should let the user know about the range allowed.

using System;


namespace SamplePrograms
{
    class ExceptionHandlingAtItsBest
    {
        public static void Main()
        {
            string strUserChoice = String.Empty;
            do
            {
                try
                {
                    Console.WriteLine("Please enter first number");
                    int FN = Convert.ToInt32(Console.ReadLine());


                    Console.WriteLine("Please enter second number");
                    int SN = Convert.ToInt32(Console.ReadLine());


                    int Total = FN + SN;


                    Console.WriteLine("Total = {0}", Total);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid Input. Only numbers please.");
                }
                catch (OverflowException)
                {
                    Console.WriteLine("Only numbers between {0} and {1} are allowed"
                        Int32.MinValue, Int32.MaxValue);
                }
                catch (Exception)
                {
                    Console.WriteLine("Unknown problem, please contact administrator");
                }


                do
                {
                    Console.WriteLine("Do you want to continue - Yes or No");
                    strUserChoice = Console.ReadLine();
                } 
                while (strUserChoice.ToUpper() != "YES" && strUserChoice.ToUpper() != "NO");
            } 
            while (strUserChoice.ToUpper() != "NO");
        }
    }
}

2 comments:

  1. Very nice article. Please also write on exception logging.

    ReplyDelete
  2. Superb article. Please write on exception logging

    ReplyDelete