int age;
try
{
age = int.Parse(ageString);
Console.WriteLine("Thank you");
}
catch (Exception e)
{
// Get the error message out of the exception
Console.WriteLine(e.Message);
}
The catch now looks like a method call, with the Exception e being a parameter to
the method. This is exactly how it works. Within the catch clause the value of e is set
to the exception that was thrown by Parse. The Exception type has a property called
Message which contains a string describing the error. If a user types in an invalid
string the program above will write the text in the exception, which contains the
message:
Input string was not in a correct format.
This message is obtained from the exception that was thrown. This is useful if the code
inside the try block could throw several different kinds of exception, since it means
that the message reflects what has actually hap