I have created a custom-exception
that i want to throw whenever a user enters a sex that is either male of female. I did this using
class sexException : Exception
{
public override string Message
{
get
{
return "Sex can only be either MALE or FEMALE";
}
}
}
Now when i want to throw the exception
in my main app, i will have to create an object of the exception and then check the value before throwing the exception.
Something like
public static void AcceptInfo()
{
Console.Write("Enter Sex : ");
string sex = Console.ReadLine();
if (sex.ToUpper() != "MALE" && sex.ToUpper() != "FEMALE")
{
try
{
sexException ne = new sexException();
throw ne;
}
catch (sexException e)
{
Console.WriteLine(e.Message);
}
}
}
My Question is; how can i create the exception
in such a way that it automatically throws when the user enters invalid data without having to just check if data is invalid like FormatException
on int
datatype.