M
M
Michael Sne Bjorn Palagin2017-07-11 12:15:32
C++ / C#
Michael Sne Bjorn Palagin, 2017-07-11 12:15:32

New to C# / Am I writing the error output correctly?

Good time friends. Perhaps the question is banal and should not be asked, but I would like to hear the opinion of people who understand C #. I am a beginner, I just recently started learning the language, I went through a lesson where they showed a way to catch an error and display it to the user. I decided to add this method to my first console application, which I did before. In it, the user had to enter a number from 1 to 5. Then I could only implement a loop that if the entered number was greater than 5 or equal to 0, then the program asked again to enter a number from 1 to 5 and so on ad infinitum until the user will do. But there was a problem that if you enter into the console not a number, but some kind of character or string, then the application gave a FormatException error and it was not buzzing :(
And having learned a little about how to catch errors and display them to the user, I decided to supplement my console application.

Here is the code:
static void Main(string[] args)
        {
            int number_user1 = 0;
            bool repeat = true;


            while (repeat)
            {

                try
                {
                    Console.Write("Player 1 write number from 1 to 5: ");
                    number_user1 = Convert.ToInt32(Console.ReadLine());
                    while (number_user1 > 5 || number_user1 == 0)
                    {
                        Console.Write("ERROR: Player 1 PLEASE write number from 1 to 5: ");
                        number_user1 = Convert.ToInt32(Console.ReadLine());
                    }
                }

                catch (FormatException format_ex)
                {
                    Console.WriteLine("Enter only NUMBERS from 1 to 5");
                }

                if (number_user1 <= 5 && number_user1 != 0)
                {
                    repeat = false;
                }

                else
                {
                    repeat = true;
                }

            }

            Console.ReadKey();
        }
I would like to know if I built the logic for capturing an error, its output, etc. correctly. (if I checked anything, everything works, I just would like to know the opinion)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Arxont, 2017-07-11
@arxont

Good afternoon.
0. The if-else block must be placed in the try block. So why compare if an error occurs?
1. In your case, it is better to use ReadKey, because by condition one character from 1 to 5.
2. It is desirable to use the finally block, but in your case it is not necessary.
3. In my opinion, there are a lot of unnecessary checks
Error handling is implemented correctly in principle, but we must remember that this is a rather heavy operation and in this case redundant. In this case, you can check through TryParse. I would decide like this -

static void Main()
{
   int number_user1 = 0;

   while (true)
   {
      Console.Write("Player 1 write number from 1 to 5: ");

      string inputChar = Console.ReadKey().KeyChar.ToString();

      if(int.TryParse(inputChar, out number_user1) && number_user1 <= 5 && number_user1 > 0)
      {
         break;
       }
       else
       {
          Console.WriteLine($"\nEnter only NUMBERS from 1 to 5 (you input {inputChar})");
       }
    }

   Console.WriteLine($"\nYou input {number_user1}");
   Console.Write("Press any key to close"); Console.ReadKey();
}

Also read - https://docs.microsoft.com/en-us/dotnet/standard/e...
https://habrahabr.ru/post/221723/
https://habrahabr.ru/post/178805/
and
https://stackoverflow.com/questions/14973642/how-u...

S
Smilley, 2017-07-11
@Smilley

Here's a little prettier and shorter:

namespace TosterApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int input;
            do Console.WriteLine("Enter number between 0 and 5:");
            while (!int.TryParse(Console.ReadLine(), out input) || input <= 0 || input > 5);
            Console.WriteLine($"You entered {input}");
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question