• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

No error

porkshopp

Member
Joined
Apr 13, 2019
Messages
10
Location
Sweden
Programming Experience
Beginner
How can I make this not output an error when a non-integer input is given?
Code:
using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = Convert.ToInt16(Console.ReadLine());
            if (a == 2 || a == 3) Console.WriteLine("Success!");
            else Console.WriteLine("Failure!");
            Main(null);
        }
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,166
Location
Sydney, Australia
Programming Experience
10+
Look into the TryParse method of the appropriate numeric type. Such methods validate and convert without throwing exceptions on failure.
 
Last edited:
Top Bottom