I have a question about Re-Entering Try Catch

patrick

Well-known member
Joined
Dec 5, 2021
Messages
251
Programming Experience
1-3
Hello

I have a question about Re-Entering Try Catch


C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int iNum1;
            int iNum2;
            while (true)
            {
                try
                {
                    Console.WriteLine("1) Please Input Integer ----1");
                    iNum1 = int.Parse(Console.ReadLine());


                    Console.WriteLine("2) Please Input Integer ----2");
                    iNum2 = int.Parse(Console.ReadLine());

                    break;
                }
                catch (Exception)
                {
                    Console.WriteLine("An integer was not entered");
                }
            }
        }
    }
}


delete2.png



Thank you
 
Pros will use int.TryParse().

Do not use try-catch for flow control. It makes code much harder to understand because of the implicit jumps that can happen. It's basically like having evil gotos without explicitly writing goto.
 
Anyway, write a helper method that uses TryParse(). The pseudo-code would look something like:

C#:
int PromptForInt(string prompt)
{
    while (true)
    {
        Console.WriteLine(prompt);
        var input = Console.WriteLine();
        if (int.TryParse(input, out int value))
            return value;
        Console.WriteLine("Invalid integer entered.");
    }
}

:

var iNum1 = PromptForInput("Enter the first number");
var iNum2 = PromptForInput("Enter the second number");
 
Check out Spectre..Console. In the code below the generic method Get only accepts int, not decimal or double. If they press ENTER the default value of 0 is set to the variable.
Code sample to get two int values:
using System;
using Spectre.Console;

namespace ConsoleApp1
{
    internal partial class Program
    {
        static void Main(string[] args)
        {
            int value1 = Get("Please enter first number", 0);
            int value2 = Get("Please enter second number", 0);
            Console.WriteLine($"{value1,-5}{value2}");
            Console.WriteLine("Press ENTER to Exit");
            Console.ReadLine();
        }

        internal static T Get<T>(string prompt, T defaultValue)
        {
            return AnsiConsole.Prompt(
                new TextPrompt<T>($"[white]{prompt}[/]")
                    .PromptStyle("white")
                    .DefaultValueStyle(new(Color.Yellow))
                    .DefaultValue(defaultValue)
                    .ValidationErrorMessage("[white on red]Invalid entry![/]"));
        }
    }
}
 
Karen probably doesn't remember the drama of past Patrick/Anwind threads.
 
Anyway, write a helper method that uses TryParse(). The pseudo-code would look something like:

C#:
var input = Console.WriteLine();

Shouldn't it be?
C#:
var input = Console.ReadLine();

I might be wrong, because I am also a beginner and might have missed something.
 
That's right. It should be ReadLine().
 

Latest posts

Back
Top Bottom