Reputation: 1
Class assignment is to create your own custom exception (MyEmptyStringException) in this case.
Here is what I have:
class Program
{
public class MyEmptyStringException : Exception
{
public MyEmptyStringException(string message) : base(message) { }
}
public class MyOutOfRangeException : Exception
{
public MyOutOfRangeException(string message) : base(message) { }
}
static void Main(string[] args)
{
string testStr;
byte testByte;
bool validValue = false;
Student myStudent = new Student();
do
{//start do loop
Console.WriteLine("Enter student's first name");
try
{
testStr = (Console.ReadLine());
if ((string.IsNullOrEmpty(testStr)))
{
throw new MyEmptyStringException("Answer was blank");
// Console.WriteLine("String is empty");
}
else
{
validValue = true;
} //end if
}
catch (FormatException e)
{
Console.WriteLine("\n" + e.Message + "Format Exception!\n");
}
catch (OverflowException e)
{
Console.WriteLine("\n" + e.Message + "Overflow Exception\n");
}
} while (!validValue);
myStudent.firstName = Console.ReadLine();
For me it errors out here:
throw new MyEmptyStringException("Answer was blank");
with the error message: MyEmptyStringException was unhandled. Any help would be appreciated.
Upvotes: 0
Views: 671
Reputation: 2382
Catch your custom exceptions:
MyEmptyStringException and MyOutOfRangeException
Eg:
try
{
}
catch (MyEmptyStringException mese)
{
Console.WriteLine(mese.Message);
}
catch (MyOutOfRangeException moofre)
{
Console.WriteLine(moofre.Message);
}
Upvotes: 2