Saturday 16 April 2016

Design a Console Application for Temperature Conversion which generates an Exception whenever the value of Temperature is zero.

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

namespace UserDefinedException
{
    class Program
    {
   
        static void Main(string[] args)
        {

            Temperature temp = new Temperature();
         
            try
            {
                temp.showTemp();
            }
            catch (ZeroException e)
            {
                Console.WriteLine("ZeroException: {0}", e.Message);
            }

            Console.ReadKey();
        }
    }
}
public class ZeroException : Exception
{
    public ZeroException(string message): base(message)
    {
    }
}

        public class Temperature
        {
           
            public void showTemp()
            {
               
                double cel;
              Console.WriteLine("Enter the temperature to convert from Fahrenheit to Celsius");
                double fah = Double.Parse(Console.ReadLine());
              cel = (fah - 32) * 5 / 9;
                Console.WriteLine("The converted temperature is" + cel + " degrees Celsius");
                if (fah == 0)
                {
                    throw (new ZeroException("Zero Temperature found"));
                }
                else
                {
                    Console.WriteLine("Temperature: {0}", cel);
                }
            }
        }

No comments:

Post a Comment