• I removed the www from the URL, please update your bookmarks and pay attention to the bottom of the screen if using Google Chrome to allow push notifications again.

No error

porkshopp

Active member
Joined
Apr 13, 2019
Messages
26
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,230
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