This code already throws a FormatException . If you mean that you want to catch him, you can write:
Console.WriteLine("Your age:"); string line = Console.ReadLine(); try { age = Int32.Parse(line); } catch (FormatException) { Console.WriteLine("{0} is not an integer", line);
However, it would be better to use int.TryParse :
Console.WriteLine("Your age:"); string line = Console.ReadLine(); if (!int.TryParse(line, out age)) { Console.WriteLine("{0} is not an integer", line);
This eliminates the exception for a rather unpredictable case of user error.
source share