0

my task is to set a certain value limit (Int32.MaxValue) for the user input. If the user exceeds this he should repeat the input. How do i get out of the exception without crashing my whole code?

This is my newbie method so far:

public bool ValidateValue()
{
        long value = Int32.Parse(UserValue);

        if (value>Int32.MaxValue)
        {
            Console.WriteLine("[ERROR] Too large number! The limit is: {0} \n Please repeat your input. ", Int32.MaxValue);
            return false;
        }
    }`         
askldq
  • 13
  • 3
  • This might be a simple typo: use `Int64.Parse` rather than `Int32.Parse`. – gunr2171 Apr 14 '23 at 12:03
  • try { } catch { } https://www.w3schools.com/cs/cs_exceptions.php – Graffito Apr 14 '23 at 12:04
  • @gunr2171 it didn't work unfortunately :/ – askldq Apr 14 '23 at 12:06
  • What doesn't work? Which line do you get an exception on, and what's the exact error message? What variable values are you working with? Give us example input values with a [mre] please. – gunr2171 Apr 14 '23 at 12:07
  • @gunr2171 i'm working with Console.ReadLine() -> UserValue and if I enter a high number, e.g. 9999999999 the statement comes up in line where i try to parse: internal static void ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type = 0) => throw GetException(status, type); – askldq Apr 14 '23 at 13:19
  • I can reproduce your problem if you use `Int32.Parse`, but not with `Int64.Parse`: https://dotnetfiddle.net/F9sw17. Please [edit] your post with a [mre]. – gunr2171 Apr 14 '23 at 15:14

0 Answers0