简体   繁体   中英

Error messages in programming in a conversion program c# Visual studio

I am new to programming and I have only been programming for about a couple of weeks and I am not sure how to make error messages for my program. Can anyone help me make a few error messages for this conversion program?

Here is the code:

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

namespace Converting_Temperature
{
class Program
{
    static double ConvertIToCentimeters()
    {
        Console.Write("Please input the Inches you would like to convert to     Centimeters: ");
        double Centimeters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Centimeters * 2.54;
        return Inches;

    }

    static double ConvertIToMillimeters()
    {
        Console.Write("Please input the Inches you would like to convert to Millimeters: ");
        double Millimeters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Millimeters * 25.4; 
        return Inches; 
    }

    static double ConvertIToMeters()
    {
        Console.Write("Please input the Inches you would like to convert to Meters: ");
        double Meters = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Meters * 0.02540000; 
        return Inches; 
    }

    static double ConvertIToFoot()
    {
        Console.Write("Please input the Inches you would like to convert to Foot: ");
        double Foot = Convert.ToDouble(Console.ReadLine());
        double Inches = (double)Foot * 0.0833333; 
        return Inches;  
    }

    static double ConvertIToHelp()
    {
        Console.Write("\n Help!\n 1.To convert from Inches to Centimetres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Centimetres.\n 2.To convert from Inches to Millimetres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Millimetres.\n 3.To convert from Inches to Feet you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Feet.\n 4.To convert from Inches to Metres you must select option 1 from the menu. The program will ask you to input the amount of inches you would like to convert. Once you enter the amount the program will convert it into Metres.\n 5.For any inquires or problems you may have about the program please contact me on my email at victor.pietrasiak@gmail.com or call this number 0123 323 324."); //tells the user about how to use the program
        double Help = Convert.ToDouble(Console.ReadLine());
        return Help; 
    }

    static int Menu()
    {
        Console.WriteLine("Please choose one of these options");
        Console.WriteLine("1.Inches to Centimeters");
        Console.WriteLine("2.Inches to Millimeters");
        Console.WriteLine("3.Inches to Meters");
        Console.WriteLine("4.Inches to Feet");
        Console.WriteLine("5.Help");
        Console.WriteLine("6.Quit");
        int choice = Convert.ToInt32(Console.ReadLine());
        return choice; 
    }






    static void Main(string[] args)
    {
        int menuChoice = Menu();
        if (menuChoice == 1)
        {
            double result = ConvertIToCentimeters();
            Console.WriteLine("The result in Centimeters is {0}", result);
        }

        if (menuChoice == 2)
        {
            double result = ConvertIToMillimeters();
            Console.WriteLine("The result in Millimeters is {0}", result);

        }

        if (menuChoice == 3)
        {
            double result = ConvertIToMeters();
            Console.WriteLine("The result in Meters is {0}", result);

        }

        if (menuChoice == 4)
        {
            double result = ConvertIToFoot();
            Console.WriteLine("The result in Foot is {0}", result);

        }

        if (menuChoice == 5)
        {
            double result = ConvertIToHelp();
            Console.WriteLine("Help {0}", result);
        }

        if (menuChoice < 1) 
        {
            throw new ApplicationException("You have entered a number less than 1"); //this was my attempt on making an error message
        }


    }

}

}

Since you are making a Console Application, you can write errors directly to the Console, as you have been doing with the normal messages.

So instead of the throw new ApplicationException("You have entered a number less than 1");

You can use:

Console.WriteLine("Error: You have entered a number less than 1");

You can output an Error like this:

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You have entered a number less than 1");
Console.ResetColor();

then your Error would be printed out in a red line, after that you could possibly close the application or restart it.

Throwing exceptions is mostly about handling really exceptional situations, which you often can't predict. In your situation, you have anticipated that the user's input might be less than 1, in which case you should just inform that the number was in some way incorrect. Use Console.WriteLine or some similar method for this.

Exceptions are more about giving developers that use your code the ability to handle unpredicted behaviour that occurred inside your methods (for example, they might want to catch an exception you threw and clean up resources) or, if the situation is very serious, to immediately shut down the application to prevent state corruption/security holes. Don't overuse them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM